2

哪种方式读取文本文件最快?1.7 的新特性是否提供了我们可以更快地读取文本文件的任何功能?

4

2 回答 2

1

我建议使用 BufferedReader,因为它的读取速度比 InputStream 之类的要快。

String filePath = ".../.../file.txt";
BufferedReader in = new BufferedReader(new FileReader(new File(pathPath)));
String line = null;
while((line = in.readLine()) != null)
    System.out.println(line);
in.close();  //very important to close streams

您还需要尝试捕获。您也可以尝试使用 Scanner,但我认为它不如 BufferedReader 快。

于 2012-09-01T04:39:36.293 回答
-1

Java 1.4+ 包括新的nio(新输入/输出)包,用于更快的文件传输和检索。考虑看一个类似的答案:Java NIO FileChannel 与 FileOutputstream 性能/实用性或 Oracle 网站上的官方示例:http: //docs.oracle.com/javase/1.4.2/docs/guide/nio/example/索引.html

于 2012-09-01T07:14:41.470 回答