我制作了一个gist,比较不同的方法:
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
String path = "resources/testfile.txt";
measureTime("BufferedReader.readLine() into LinkedList", Main::bufferReaderToLinkedList, path);
measureTime("BufferedReader.readLine() into ArrayList", Main::bufferReaderToArrayList, path);
measureTime("Files.readAllLines()", Main::readAllLines, path);
measureTime("Scanner.nextLine() into ArrayList", Main::scannerArrayList, path);
measureTime("Scanner.nextLine() into LinkedList", Main::scannerLinkedList, path);
measureTime("RandomAccessFile.readLine() into ArrayList", Main::randomAccessFileArrayList, path);
measureTime("RandomAccessFile.readLine() into LinkedList", Main::randomAccessFileLinkedList, path);
System.out.println("-----------------------------------------------------------");
}
private static void measureTime(String name, Function<String, List<String>> fn, String path) {
System.out.println("-----------------------------------------------------------");
System.out.println("run: " + name);
long startTime = System.nanoTime();
List<String> l = fn.apply(path);
long estimatedTime = System.nanoTime() - startTime;
System.out.println("lines: " + l.size());
System.out.println("estimatedTime: " + estimatedTime / 1_000_000_000.);
}
private static List<String> bufferReaderToLinkedList(String path) {
return bufferReaderToList(path, new LinkedList<>());
}
private static List<String> bufferReaderToArrayList(String path) {
return bufferReaderToList(path, new ArrayList<>());
}
private static List<String> bufferReaderToList(String path, List<String> list) {
try {
final BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null) {
list.add(line);
}
in.close();
} catch (final IOException e) {
e.printStackTrace();
}
return list;
}
private static List<String> readAllLines(String path) {
try {
return Files.readAllLines(Paths.get(path));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static List<String> randomAccessFileLinkedList(String path) {
return randomAccessFile(path, new LinkedList<>());
}
private static List<String> randomAccessFileArrayList(String path) {
return randomAccessFile(path, new ArrayList<>());
}
private static List<String> randomAccessFile(String path, List<String> list) {
try {
RandomAccessFile file = new RandomAccessFile(path, "r");
String str;
while ((str = file.readLine()) != null) {
list.add(str);
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
private static List<String> scannerLinkedList(String path) {
return scanner(path, new LinkedList<>());
}
private static List<String> scannerArrayList(String path) {
return scanner(path, new ArrayList<>());
}
private static List<String> scanner(String path, List<String> list) {
try {
Scanner scanner = new Scanner(new File(path));
while (scanner.hasNextLine()) {
list.add(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return list;
}
}
运行:BufferedReader.readLine() 到 LinkedList,
行数:1000000,
估计时间:0.105118655
运行:BufferedReader.readLine() 到 ArrayList,
行数:1000000,
估计时间:0.072696934
运行:Files.readAllLines(),
行数:1000000,
估计时间:0.087753316
运行:Scanner.nextLine() 到 ArrayList,
行数:1000000,
估计时间:0.743121734
运行:Scanner.nextLine() 到 LinkedList,
行数:1000000,
估计时间:0.867049885
运行:RandomAccessFile.readLine() 到 ArrayList,
行数:1000000,
估计时间:11.413323046
运行:RandomAccessFile.readLine() 到 LinkedList,
行数:1000000,
估计时间:11.423862897
BufferedReader
是最快的,Files.readAllLines()
也可以接受,Scanner
因为正则表达式而较慢,RandomAccessFile
不可接受。