简体   繁体   English

使用BufferedReader从文件读取一组行

[英]Read a set of lines from a file using BufferedReader

Hi I have one large csv file of around 1.6 GB and I am trying to read that file and create json array of these lines and send it to other consumables processes. 嗨,我有一个大约1.6 GB的大型csv文件,我正在尝试读取该文件并创建这些行的json数组,并将其发送给其他消耗品流程。

I have the following code 我有以下代码

while(consumeover){
    try (BufferedReader br = new BufferedReader(new FileReader
                                ("/my/path/largefile"),65536)) {
         for (String line; (line = br.readLine()) != null;) {
         String[] dataRow = line.split("\\|");
         //create json array
         //add each dataRow element to array  
         }
    }
   }

Now what is happening is above code reads entire file and creates json array which throws Out of Memory error. 现在发生的是上面的代码读取整个文件并创建json数组,该数组抛出内存不足错误。 I want to read set of lines say 1000 lines every time I create json array. 我想每次我创建json数组时都要读取1000行。 How do I set my last read position to i+1000? 如何将上次读取位置设置为i + 1000? As file is very huge Java is throwing out of memory because of data arrays getting created. 由于文件非常大,由于创建了数据数组,Java耗尽了内存。

Please guide thanks in advance. 请提前指导谢谢。

The simple solution is to output each line as you get it (not save it in an array and then send/write it), or every 1000 lines as you get them. 简单的解决方案是在获取时输出每行(而不是将其保存在数组中,然后发送/写入),或者在获取时每输出1000行。 This way you only read the file in one go. 这样,您只需要一次性读取文件。 The less line you hold in memory, the less memory you use. 您在内存中保留的行越少,您使用的内存就越少。

Note: the only way to read to from line N is to read N lines and ignore them. 注意:从N行读取数据的唯一方法是读取N行并忽略它们。 This would become increasingly expensive as the file get larger. 随着文件变大,这将变得越来越昂贵。

Say you have a method which translates a line of CSV into JSon. 假设您有一种将CSV线转换为JSon的方法。

try(BufferedReader br = new BufferedReader(new FileReader(infile));
    PrintWriter bw = new PrintWriter(new FileWriter(outfile))) {
    for(String line; (line = br.readLine()) != null;) {
        String json = process(line);
        bw.println(json);
    }
}

This will only need enough memory for one line of CSV and one line of JSON, no matter how big the file is. 不管文件有多大,这仅需要足够的内存来存储一行CSV和一行JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM