Here is another example to show how to read a file in Java with BufferedInputStream and DataInputStreamclasses.
P.S readLine() from the type DataInputStream is deprecated. Sun officially announced this method can not convert property from bytes to characters. It’s advised to use BufferedReader
查看: java读取文本文件代码
- import java.io.BufferedInputStream;
-
import java.io.DataInputStream;
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.IOException;
-
-
public class UseBufferedInputStream {
-
-
public static void main(String[] args) {
-
-
File file = new File("C:\\readFile.txt");
-
FileInputStream fis = null;
-
BufferedInputStream bis = null;
-
DataInputStream dis = null;
-
-
try {
-
fis = new FileInputStream(file);
-
-
bis = new BufferedInputStream(fis);
-
dis = new DataInputStream(bis);
-
-
while (dis.available() != 0) {
-
System.out.println(dis.readLine());
-
}
-
-
fis.close();
-
bis.close();
-
dis.close();
-
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
阅读(957) | 评论(0) | 转发(0) |