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)并行处理肯定会提高速度。创建一个大字节缓冲区,运行多个任务,将文件中的字节并行读取到该缓冲区中,当准备好找到行的第一行时,创建一个
String,然后查找下一个…



