How can I read a large text file line by line using Java?

前端 未结 21 2396
心在旅途
心在旅途 2020-11-21 05:48

I need to read a large text file of around 5-6 GB line by line using Java.

How can I do this quickly?

21条回答
  •  一生所求
    2020-11-21 06:24

    For reading a file with Java 8

    package com.java.java8;
    
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.stream.Stream;
    
    /**
     * The Class ReadLargeFile.
     *
     * @author Ankit Sood Apr 20, 2017
     */
    public class ReadLargeFile {
    
        /**
         * The main method.
         *
         * @param args
         *            the arguments
         */
        public static void main(String[] args) {
            try {
                Stream stream = Files.lines(Paths.get("C:\\Users\\System\\Desktop\\demoData.txt"));
                stream.forEach(System.out::println);
            }
            catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    

提交回复
热议问题