在java中将大文件加载到arraylist的最佳方法 [英] Best way to load a large file into arraylist in java

查看:100
本文介绍了在java中将大文件加载到arraylist的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大小约为300mb的文件。我想逐行读取内容,然后将其添加到ArrayList中。所以我创建了一个数组列表a1的对象,然后使用BufferedReader读取文件,之后当我将文件中的行添加到ArrayList中时,它在线程main中给出错误异常java.lang.OutOfMemoryError:Java堆空间。

I have a file whose size is about 300mb. I want to read the contents line by line and then add it into ArrayList. So I have made an object of array list a1 , then reading the file using BufferedReader , after that when I add the lines from file into ArrayList it gives an error Exception in thread "main" java.lang.OutOfMemoryError: Java heap space.

请告诉我这个解决方案应该是什么。

Please tell me what should be the solution for this.

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
      FileReader file = new FileReader(
          "/home/dmdd/Desktop/AsiaData/RawData/AllupperairVcomponent.txt");
      ArrayList a1 = new ArrayList();
      BufferedReader br = new BufferedReader(file);
      String line = "";
      while ((line = br.readLine()) != null) {
        a1.add(line);
      }
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }


推荐答案

增加Java的堆大小

java -Xmx250m

java -Xmx250m

如果从IDE set -Xmx250m运行项目参数。

If you running your project from IDE set -Xmx250m in arguments.

250m是250mb

250m is 250mb

这篇关于在java中将大文件加载到arraylist的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆