使用apache common io读取大文件

P b*_*sak 2 java string io parsing

您好我使用以下代码使用FileUtils.readLinesApache commons IO库中的功能获取字符串列表中的所有行.这是我的代码,

List<String> lines=FileUtils.readLines(new File(fileName));
Run Code Online (Sandbox Code Playgroud)

但每当我发送一个文件说45MB,100万行时,它会给我一个内存不足的错误.应该是什么解决方案.我需要处理每一行.

Ren*_*ink 10

读一行接一行

LineIterator it = FileUtils.lineIterator(file, "UTF-8");
 try {
   while (it.hasNext()) {
    String line = it.nextLine();
     // do something with line
   }
 } finally {
  it.close();
 } 
Run Code Online (Sandbox Code Playgroud)