Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中读取大型CSV_Java_File Io_Opencsv - Fatal编程技术网

在java中读取大型CSV

在java中读取大型CSV,java,file-io,opencsv,Java,File Io,Opencsv,我想从CSV读取巨大的数据,包含大约500000行。 我正在使用OpenCSV库。我的代码是这样的 CsvToBean<User> csvConvertor = new CsvToBean<User>(); List<User> list = null; try { list =csvConvertor.parse(strategy, new BufferedReader(new FileReader(filepath)

我想从CSV读取巨大的数据,包含大约500000行。 我正在使用OpenCSV库。我的代码是这样的

    CsvToBean<User> csvConvertor = new CsvToBean<User>();
    List<User> list = null;
    try {
        list =csvConvertor.parse(strategy, new BufferedReader(new FileReader(filepath)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
    String [] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        // nextLine[] is an array of values from the line
        System.out.println(nextLine[0] + nextLine[1] + "etc...");
    }
我在“eclipse.ini”文件中有这个内存设置

我正在考虑一个解决方案,将这个巨大的文件拆分成不同的文件,然后再次读取这些文件,我认为这是一个冗长的解决方案


有没有其他方法可以避免OutOfMemoryError异常。

您必须为应用程序设置
-Xmx
值,在这种情况下不能设置eclipse。在“运行配置”中,选择您的应用程序,然后转到“参数”选项卡,并在“VM参数”中设置该值,例如
-Xmx1024m
。 通过右键单击要运行的文件,然后选择运行方式,然后选择“运行配置…”,可以打开运行配置。

逐行读取

像这样的

    CsvToBean<User> csvConvertor = new CsvToBean<User>();
    List<User> list = null;
    try {
        list =csvConvertor.parse(strategy, new BufferedReader(new FileReader(filepath)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
    String [] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        // nextLine[] is an array of values from the line
        System.out.println(nextLine[0] + nextLine[1] + "etc...");
    }

通过以下示例,您从csv文件中读取了n条记录。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadCSV 
{
    public static void main(String[] args) 
    {
        String csvFile = "C:/Users/LENOVO/Downloads/Compressed/GeoIPCountryWhois.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        try 
        {
            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) 
            {
                // use comma as separator
                String[] country = line.split(cvsSplitBy);

                System.out.println("Country [code= " + country[4] + " , name=" + country[5] + "]");
            }

        }
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        } 
        finally 
        {
            if (br != null) 
            {
                try 
                {
                    br.close();
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("Done");
    }
}

看这个-为什么你需要在内存中保存200k对象?你不能读取更小的子集吗?你们在用对象列表做什么?看看csvConverter.parse做了什么,然后重新实现它。应该不会太多。让该方法重新运行一个迭代器,这样你就可以在迭代时进行解析。你可以尝试将文件的全部内容保存在内存中(在列表中)。如果你真的需要这个:买更多的内存。否则,每行或以较小的集合读取/处理条目。我收到此消息-VM初始化期间发生错误,无法为对象堆保留足够的空间。在“运行配置”中添加-Xmx1024m后,您没有足够的RAM+交换空间用于您希望保留的内存+所有运行程序占用的内存。尝试将其设置为较低的值,例如512m并关闭一些不必要的程序。println完全没有意义。这也不是他的问题的答案。他需要将数据转换成用户对象,如果他同时需要所有20万个对象,逐行读取也无济于事。没错。这只是一个例子。您可以保留数据以供进一步处理(即批处理)或执行任何操作,而不是将结果打印到控制台want@urbiwanus如何重用CSVReader对象,而不是多次创建它。