【问题标题】:java: reading large file with charsetjava:使用字符集读取大文件
【发布时间】:2015-03-04 15:06:13
【问题描述】:

我的文件是 14GB,我想逐行读取并将导出到 excel 文件。

由于文件包含不同的语言,例如中文和英文,
我尝试使用FileInputStreamUTF-16 来读取数据,
但导致java.lang.OutOfMemoryError: Java 堆空间
我已尝试增加堆空间,但问题仍然存在
我应该如何更改我的文件读取代码?

createExcel();     //open a excel file
try {

    //success but cannot read and output for different language
    //br = new BufferedReader(
    //        new FileReader("C:\\Users\\brian_000\\Desktop\\appdatafile.json"));


    //result in java.lang.OutOfMemoryError: Java heap space
    br = new BufferedReader(new InputStreamReader(
            new FileInputStream("C:\\Users\\brian_000\\Desktop\\appdatafile.json"), 
            "UTF-16"));

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 

System.out.println("cann be print");


String line;
int i=0;
try {
    while ((line = br.readLine()) != null) {
        // process the line.
        try{
            System.out.println("cannot be print");
            //some statement for storing the data in variables.



                   //a function for writing the variable into excel
writeToExcel(platform,kind,title,shareUrl,contentRating,userRatingCount,averageUserRating
                            ,marketLanguage,pricing
                            ,majorVersionNumber,releaseDate,downloadsCount);


            }
            catch(com.google.gson.JsonSyntaxException exception){
                System.out.println("error");
            }



            // trying to get the first 1000rows
            i++;

            if(i==1000){
                br.close();

                break;
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    closeExcel();




public static void writeToExcel(String platform,String kind,String title,String shareUrl,String contentRating,String userRatingCount,String averageUserRating
            ,String marketLanguage,String pricing,String majorVersionNumber,String releaseDate,String downloadsCount){

        currentRow++;
        System.out.println(currentRow);

        if(currentRow>1000000){
            currentsheet++;
            sheet = workbook.createSheet("apps"+currentsheet, 0);
            createFristRow();
            currentRow=1;
        }



        try {

                //character id
                Label label = new Label(0, currentRow, String.valueOf(currentRow), cellFormat);
                sheet.addCell(label);

                //12 of statements for write the data to excel
                label = new Label(1, currentRow, platform, cellFormat);
                sheet.addCell(label);




            } catch (WriteException e) {
                e.printStackTrace();
            }

【问题讨论】:

  • 仅此代码不应导致 OOM;请发布完整的代码。另外,如果您使用 Java 7+,请删除File 并使用 java.nio.file。
  • “由于文件包含不同的语言,例如中文和英文,我尝试使用带有 UTF-16 的 FileInputStream 来读取数据” - 文件实际上是 UTF -16?如果不检查它是否正确,则不应使用编码。你是否坚持读过的台词?
  • 这里是否抛出了错误? br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\brian_000\\Desktop\\appdatafile.json"), "UTF-16"));
  • 我已经编辑了我的代码,我认为 readLine() 有问题。我不确定它是否使用 UTF-16,只是我通过读取为 utf16 来解决类似的问题,我如何检查它使用的是哪种编码?
  • 我觉得你的文件读取代码不错。问题将出在 Excel 文件上……writeToExcel 会发生什么?我猜 Excel 数据结构会在内存中增长,直到整个事情崩溃。

标签: java file


【解决方案1】:

Excel,UTF-16

如前所述,问题可能是由 Excel 文档构造引起的。试试 UTF-8 是否会产生更小的尺寸;例如,由于 ASCII 字符很多,使用 UTF-8 而不是 UTF-16 压缩中文 HTML 仍然更好。

对象创建java

您可以share common small Strings。对String.valueOf(row) 等有用。仅缓存长度较小的字符串。我假设 cellFormat 是固定的。

用 xlsx DIY

Excel 构建了一个昂贵的 DOM。 如果没有选项 CSV 文本(带有 Unicode BOM 标记)(您可以为其提供扩展名 .xls 以由 Excel 打开),请尝试生成 xslx。 在 xslx 中创建示例工作簿。 这是一种 zip 格式,您可以使用 zip filesystem 在 java 中轻松处理。 对于 Excel,有一个内容 XML 和一个共享 XML,通过从内容到共享字符串的索引共享单元格值。 然后在您按缓冲区写入时不会发生溢出。 或者使用 Excel 的 JDBC 驱动程序。 (我最近没有经验,可能是 JDBC/ODBC。)

最佳

Excel 很难处理这么多数据。考虑使用数据库付出更多的努力,或者将每 N 行写入适当的 Excel 文件。也许你以后可以import他们在一个文档中使用java。 (我怀疑。)

【讨论】:

  • 我改成UTF-8后问题解决了。你介意推荐一个数据库软件吗?如果数据可以通过数据库软件 .exe 的 .db 访问,那就太好了
  • 我更喜欢嵌入式数据库,存储为文件,用于“小型”隔离应用程序。 h2database 浮现在脑海中。
猜你喜欢
  • 1970-01-01
  • 2011-01-22
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 2020-03-10
相关资源
最近更新 更多