1)我确定速度没有差别,两者都在内部使用FileInputStream和缓冲

2)您可以自己测量并看看

3)虽然没有表现好处,我喜欢1.7的方法

try (BufferedReader br = Files.newBufferedReader(Paths.get("test.txt"), StandardCharsets.UTF_8)) {
for (String line = null; (line = br.readLine()) != null;) {
//
}
}

4)基于扫描仪的版本

try (Scanner sc = new Scanner(new File("test.txt"), "UTF-8")) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
}
// note that Scanner suppresses exceptions
if (sc.ioException() != null) {
throw sc.ioException();
}
}

5)这可能比其他的更快

try (SeekableByteChannel ch = Files.newByteChannel(Paths.get("test.txt"))) {
ByteBuffer bb = ByteBuffer.allocateDirect(1000);
for(;;) {
StringBuilder line = new StringBuilder();
int n = ch.read(bb);
// add chars to line
// ...
}
}

它需要一点编码,但由于ByteBuffer.allocateDirect,它可能会更快。它允许操作系统直接从文件读取字节到ByteBuffer,而无需复制

6)并行处理肯定会增加速度。制作一个大字节缓冲区,运行几个任务,从文件读取字节到并行缓冲区,当准备好找到第一行,做一个字符串,找到下一个…