用Java.util.Scanner 扫描文件的每一行,一行一行连续的读取:

FileInputStream inputStream = null;
Scanner sc = null;try {
    inputStream = new FileInputStream(path);
    sc = new Scanner(inputStream, "UTF-8");    
while (sc.hasNextLine()) {        
String line = sc.nextLine();        // System.out.println(line);
    }    // note that Scanner suppresses exceptions
    if (sc.ioException() != null) {        throw sc.ioException();
    }
} finally {    if (inputStream != null) {
        inputStream.close();
    }    if (sc != null) {
        sc.close();
    }
}

这种方案会遍历文件中的所有行,允许对每一行进行处理,而不保持对它的引用,总之没有把他们存放在内存中,我们可以看到,大约消耗了150MB内存。

同样可以使用Commons IO流,利用该库提供的自定义 LineIterator类:

LineIterator it = FileUtils.lineIterator(theFile, "UTF-8");try {    while (it.hasNext()) {        String line = it.nextLine();        // do something with line
    }
} finally {
    LineIterator.closeQuietly(it);
}

同样也消耗了相当少的内存,大约150M: