java 一行行读取文件_Java中按行读取文件

本文译自Java read a file line by line – How Many Ways? 转载请注明出处。

Java中的I/O类非常多,什么时候该用什么类经常让人困惑。下面两段代码展示了如何一行一行的读取一个文件:

方法1:

private static void readFile1(File fin) throws IOException {

FileInputStream fis = new FileInputStream(fin);

//Construct BufferedReader from InputStreamReader

BufferedReader br = new BufferedReader(new InputStreamReader(fis));

String line = null;

while ((line = br.readLine()) != null) {

System.out.println(line);

}

br.close();

}

方法2:

private static void readFile2(File fin) throws IOException {

// Construct BufferedReader from FileReader

BufferedReader br = new BufferedReader(new FileReader(fin));

String line = null;

while ((line = br.readLine()) != null) {

System.out.printl

本文链接:https://my.lmcjl.com/post/14533.html

展开阅读全文

4 评论

留下您的评论.