繁体   English   中英

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

[英]java: reading large file with charset

我的文件为14GB,我想逐行阅读,并将其导出为ex​​cel文件。

由于文件包含不同的语言,例如中文和英文,
我尝试将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-16更好地用UTF-8压缩。

对象创建Java

您可以共享常见的小String 对于String.valueOf(row)等有用。 仅缓存长度较小的字符串。 我认为cellFormat是固定的。

用xlsx DIY

Excel构建昂贵的DOM。 如果没有CSV文本(带有Unicode BOM标记)(您可以给它扩展名.xls以由Excel打开),请尝试生成xslx。 在xslx中创建示例工作簿。 这是一种zip格式,您可以使用zip文件系统最简单地用java处理。 对于Excel,有一个内容XML和一个共享XML,使用从内容到共享字符串的索引共享单元格值。 这样,在以缓冲区方式编写时就不会发生溢出。 或将JDBC驱动程序用于Excel。 (我这边没有最近的经验,也许是JDBC / ODBC。)

最好

Excel很难使用那么多数据。 考虑使用数据库做更多的工作,或者在适当的Excel文件中每隔N行写入一次。 也许您以后可以在一个文档中使用Java 导入它们。 (我对此表示怀疑。)

暂无
暂无

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

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