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
これにより、OS はバイトをコピーせずにファイルから直接読み取ることができます。
6) 並列処理は間違いなく速度を向上させます。大きなバイトバッファを作成し、ファイルからそのバッファにバイトを並行して読み取るいくつかのタスクを実行し、準備ができたら最初の行末を見つけ、を作成しString
、次を検索...