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

前端 未结 21 2434
心在旅途
心在旅途 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 06:15

    Here is a sample with full error handling and supporting charset specification for pre-Java 7. With Java 7 you can use try-with-resources syntax, which makes the code cleaner.

    If you just want the default charset you can skip the InputStream and use FileReader.

    InputStream ins = null; // raw byte-stream
    Reader r = null; // cooked reader
    BufferedReader br = null; // buffered for readLine()
    try {
        String s;
        ins = new FileInputStream("textfile.txt");
        r = new InputStreamReader(ins, "UTF-8"); // leave charset out for default
        br = new BufferedReader(r);
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
    }
    catch (Exception e)
    {
        System.err.println(e.getMessage()); // handle exception
    }
    finally {
        if (br != null) { try { br.close(); } catch(Throwable t) { /* ensure close happens */ } }
        if (r != null) { try { r.close(); } catch(Throwable t) { /* ensure close happens */ } }
        if (ins != null) { try { ins.close(); } catch(Throwable t) { /* ensure close happens */ } }
    }
    

    Here is the Groovy version, with full error handling:

    File f = new File("textfile.txt");
    f.withReader("UTF-8") { br ->
        br.eachLine { line ->
            println line;
        }
    }
    

提交回复
热议问题