java:使用字符集读取大文件

标签 java file

我的文件有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();
            }

最佳答案

Excel、UTF-16

如上所述,该问题很可能是由 Excel 文档构造引起的。尝试 UTF-8 是否会产生较小的大小;例如,由于存在许多 ASCII 字符,中文 HTML 仍然使用 UTF-8 压缩比使用 UTF-16 更好。

对象创建java

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

使用 xlsx DIY

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

最佳

Excel 很难处理这么多数据。考虑更多地使用数据库,或者将每 N 行写入适当的 Excel 文件中。也许你可以稍后import它们与java在一个文档中。 (我对此表示怀疑。)

关于java:使用字符集读取大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28857790/

相关文章:

java - 从多个 Activity 在模型中设置数据

java - Hibernate 拦截器 - 加载事件后

c++ - 程序计算辅音错误

html - 输入类型=文件 "Accept"属性在 Chrome 中不起作用?

java - 使用 jetty-runner 提供网络共享目录

java - Hibernate 无法区分同名的列

javascript - 从 node.js 服务器下载文件

linux - 从具有多字符分隔符的文本文件中提取列,即 "%$%"

java - Exception Java API 是否违反了 Liskov 原则?

java - 如何在 Android 中打开具有 Uri 的文件?