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

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

    Look at this blog:

    • Java Read File Line by Line - Java Tutorial

    The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

    // Open the file
    FileInputStream fstream = new FileInputStream("textfile.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    
    String strLine;
    
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   {
      // Print the content on the console
      System.out.println (strLine);
    }
    
    //Close the input stream
    fstream.close();
    

提交回复
热议问题