import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
long startTime = System.nanoTime();
try (BufferedReader reader = new BufferedReader(new FileReader("largefile.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
// Process the line
}
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.nanoTime();
System.out.println("BufferedReader Time: " + (endTime - startTime) + " ns");
}
}
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class ApacheCommonsIOExample {
public static void main(String[] args) {
long startTime = System.nanoTime();
File file = new File("largefile.txt");
try {
List<String> lines = FileUtils.readLines(file, "UTF-8");
// Process the lines
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.nanoTime();
System.out.println("Apache Commons IO Time: " + (endTime - startTime) + " ns");
}
}
For large files: BufferedReader might offer better performance due to lower overhead.
For many small files: BufferedReader can be more efficient as the overhead of Apache Commons IO might add up.
For convenience and simplicity: Apache Commons IO is often preferred despite the potential slight performance hit.
具体结果明天给