What is the best file reading (Java)

There are many ways to read Java files, but I measured which one was the fastest. This time, I measured by reading all lines.

Target

1.Files.readAllLines Read all lines of a file using readAllLines of java.nio.file.Files introduced from Java7. Internally, it just creates a BufferedReader, reads it line by line, sets it in a List, and returns it.

try {
    List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
} catch (IOException e) {
    e.printStackTrace();
}
  1. Use FileReader. This is the traditional method. Read the File with FileReader and wrap it with BufferedReader. For the character code, the character code of the environment is used as it is. If you want to specify the character code, you cannot use it.
try (BufferedReader reader = new BufferedReader(new FileReader(file));){
    String str;
    List<String> lines = new ArrayList<>();
    while ((str = reader.readLine()) != null) {
        lines.add(str);
    }
} catch (IOException e) {
    e.printStackTrace();
}
  1. Use FileInputStream. This is the method used when the character code is used in the conventional way. Read the file with FileInputStream and specify the character code with InputStreamReader. Then wrap it in BufferedReader.
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));){
    String str;
    List<String> lines = new ArrayList<>();
    while ((str = reader.readLine()) != null) {
        lines.add(str);
    }
} catch (IOException e) {
    e.printStackTrace();
}
  1. Use Files.readAllLines. A function added from Java8 that returns all lines of a file as a Stream. The values are listed in the termination process.
try {
    List<String> lines = Files.lines(path, StandardCharsets.UTF_8).collect(Collectors.toList());
} catch (IOException e) {
    e.printStackTrace();
}

I tried to run

I tried running it with the following source TestFile is a 200,000 line text file.

private static final String PATH = "src/test/java/com/test/common/TestFile";

public static void main(String[] args) {
    Path path = Paths.get(PATH);
    File file = path.toFile();
    // Files.ReadAllLines measurement
    Runnable test1 = () -> {
        try {
            List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
    };

    //BufferedReader Measurement of FileReader
    Runnable test2 = () -> {
        try (BufferedReader reader = new BufferedReader(new FileReader(file));){
            String str;
            List<String> lines = new ArrayList<>();
            while ((str = reader.readLine()) != null) {
                lines.add(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    };

    //BufferedReader InputStreamReader FileInputStream measurement
    Runnable test3 = () -> {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));){
            String str;
            List<String> lines = new ArrayList<>();
            while ((str = reader.readLine()) != null) {
                lines.add(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    };

    // Files.ReadAllLines measurement
    Runnable test4 = () -> {
        try {
            List<String> lines = Files.lines(path, StandardCharsets.UTF_8).collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
    };


    calcFuncTime(test1);
    calcFuncTime(test2);
    calcFuncTime(test3);
    calcFuncTime(test4);
}

/**
 *Measure time
 * @param runnable
 */
private static void calcFuncTime(Runnable runnable) {
    long start = System.nanoTime();
    runnable.run();
    long end = System.nanoTime();
    System.out.println(end - start);
}

Execution result


101206619
178792154
53933069
102386121

What is the best

When checking the execution results, the slowest case was the second case, and the earliest was the third case. However, the difference is about 0.1 seconds, so if there is a difference of about 200,000 lines, I think that you should hardly worry about it. Personally, I don't recommend using FileReader because it doesn't use character encoding and is slow. The method using FileInputStream is the fastest, but since the amount of description is large, the method using Files is probably the best. Please excuse me.

Recommended Posts

What is the best file reading (Java)
What is java
What is Java <>?
What is Java
What is the main method in Java?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
What is the Java Servlet / JSP MVC model?
What is the volatile modifier for Java variables?
What is Java Assertion? Summary.
The migration file is duplicated
What is the BufferedReader class?
What is a jar file?
[JAVA] What is the difference between interface and abstract? ?? ??
What is a Java collection?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
What is the constructor for?
What is the difference between Java EE and Jakarta EE?
[Java basics] What is Class?
What is java escape analysis?
Java is the 5th day
What is the initialize method?
What is the LocalDateTime class? [Java beginner] -Date and time class-
What is JVM (Java Virtual Machine)?
'% 02d' What is the percentage of% 2?
Where is the Java LocalDateTime.now () timezone?
Unzip the zip file in Java
What is thread safe (with Java)
[Java] What is Concurrent Modification Exception?
What is a lambda expression (Java)
[Java] What is the difference between form, entity and dto? [Bean]
What is a class in Java language (3 /?)
What is a Spring Boot .original file?
What is the right migration data type? ??
What is testing? ・ About the importance of testing
What is a class in Java language (1 /?)
What is Java and Development Environment (MAC)
What is a class in Java language (2 /?)
What is the model test code testing
What are the updated features of java 13
What is the data structure of ActionText?
The Java EE Security API is here!
What is the Facade pattern useful for?
Java: Place the ResourceBundle properties file anywhere
[Java8] Search the directory and get the file
Java vs. JavaScript: What ’s the Difference?
Memo: [Java] If a file is in the monitored directory, process it.
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
What is JSP? ~ Let's know the basics of JSP !! ~
What is Cubby
What is Docker?
JAVA: jar, aar, view the contents of the file
What surprised the Java shop when writing PHP
What is maven?
The order of Java method modifiers is fixed
The ruby version is managed in the .rbenv / version file
What is Jackson?