Java 单字节、多字节读取文本文档中的内容

时间:2022-08-15 04:02:48

参考1:Java中try()catch{}的使用方法

参考2:The try-with-resources Statement

文本文档位于工程下,使用鼠标右键点击工程,选择new -> File,即可创建。

Java 单字节、多字节读取文本文档中的内容

文本文档的格式:GBK

Java 单字节、多字节读取文本文档中的内容

例1:单字节读取

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; public class Main { public static void main(String[] args) {
System.out.println(System.getProperty("user.dir")); File file = new File("text.txt");
InputStream inputStream = null; try {
if ((file.exists()) && (file.isFile())) {
inputStream = new FileInputStream(file);
int data = -1;
do {
data = inputStream.read();
if (data != -1) {
System.out.print((char) data);
} else {
System.out.print(data);
}
} while (data != -1);
System.out.println();
} else if (!file.exists()) {
System.out.println("The " + file.getName() + " does not exist.");
} else if (!file.isFile()) {
System.out.println("The " + file.getName() + " is not a file.");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
// Closes this input stream and releases any system resources associated with the stream.
inputStream.close();
System.out.println("Close the input stream.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

改写例1:

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; public class Main { public static void main(String[] args) {
System.out.println(System.getProperty("user.dir")); File file = new File("text.txt"); try (InputStream inputStream = new FileInputStream(file)) {
if ((file.exists()) && (file.isFile())) {
int data = -1;
do {
data = inputStream.read();
if (data != -1) {
System.out.print((char) data);
} else {
System.out.print(data);
}
} while (data != -1);
System.out.println();
} else if (!file.exists()) {
System.out.println("The " + file.getName() + " does not exist.");
} else if (!file.isFile()) {
System.out.println("The " + file.getName() + " is not a file.");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

多字节读取

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; public class IOTest02 { public static void main(String[] args) {
File src = new File("src.txt");
InputStream is = null; try {
is = new FileInputStream(src);
byte[] buffer = new byte[1024 * 1]; // 1k bytes
int length = -1;
while ((length = is.read(buffer)) != -1) {
String str = new String(buffer, 0, length); // decode
System.out.print(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
System.out.println("\n\nInputStream Closed.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}