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

前端 未结 21 2385
心在旅途
心在旅途 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:15

    You can use this code:

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ReadTextFile {
    
        public static void main(String[] args) throws IOException {
    
            try {
    
                File f = new File("src/com/data.txt");
    
                BufferedReader b = new BufferedReader(new FileReader(f));
    
                String readLine = "";
    
                System.out.println("Reading file using Buffered Reader");
    
                while ((readLine = b.readLine()) != null) {
                    System.out.println(readLine);
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    

提交回复
热议问题