简体   繁体   English

用NIO写一个大文本文件

[英]Write a large text file with NIO

I write a large, approximately 1200 (+/- 200) mb , csv file for an offline report in my application. 我在应用程序中为脱机报告编写了一个大的,大约1200(+/- 200)mb的csv文件。 (A thread performs this job.) The data count may run around 50 million, so the query is run for every 50k rows. (一个线程执行此工作。)数据计数可能会达到5000万左右,因此查询每50k行运行一次。 The query loop runs till empty fetch (for the given criteria). 查询循环一直运行到空获取(对于给定条件)为止。 To write the data to the file, instead of using the Java streams, I tried the nio. 为了将数据写入文件,而不是使用Java流,我尝试了nio。 It took me ~12 seconds to write a huge string with 50000 lines. 我花了大约12秒的时间写了一个包含50000行的巨大字符串。 The same code tried with BufferedWriter took around ~18-22 seconds. 使用BufferedWriter尝试的相同代码大约花费了18-22秒。 The nio approach code is given below. nio方法代码如下。 I want to know if this is the straight forward way to use the nio in writing a huge file? 我想知道这是否是直接使用nio编写巨大文件的方法? Anything I have overlooked, missed? 我有什么遗漏吗? Any other way, optimization and code improvement is welcome. 任何其他方式,都可以优化和改进代码。

private static void writeData(FileChannel channel, String data) {
    ByteBuffer buffer = null;
    try {
        buffer = Charset.forName("UTF-8").encode(
                CharBuffer.wrap(data.toCharArray()));
        channel.write(buffer);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private String writeReport() {
    try {
        FileOutputStream out = new FileOutputStream(pathToFile, true);
        FileChannel channel = out.getChannel();
        // db query
        while(iterate resultset) {
             // get row result
            writeData(channel, data);
        }
    } catch(Exception e){
      //log
    } finally {
      channel.close();
      out .close();
    }
}

//pseudo code with bufferedwriter
private String writeReport(Resultset rs, String file) {
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file), 1024 * 10);
        int headerCount = 0;
        while(rs.next()) {
            String col1 = rs.getString(1);
            String col2 = rs.getString(2);
            String col3 = rs.getString(3);
            String col4 = rs.getString(4);
            String col5 = rs.getString(5);
            String colN= rs.getString(n); //nth column
            if(headerCount==0) {
                writeHeader(writer);
                headerCount++;
            }
            if(col1.equals(condition1)) {
                 writer.append(col1).append(",");
            }
            ......
            if(colN.equals(conditionN)) {
                 writer.append(colN).append(",").append(newLine());
            }
        }
    } catch(Exception e){
      //log
    } finally {
      writer.close();
    }
}

The fastest way to write your file would probably be with a BufferedWriter. 写入文件最快的方法可能是使用BufferedWriter. If that was slow I would want to see your code. 如果那很慢,我希望查看您的代码。 NIO shouldn't be expected to deliver anything startling here, and the code you posted certainly won't be faster than a BufferedWriter, as it will do many more physical writes. 不应期望NIO在这里提供任何令人吃惊的东西,并且您发布的代码当然不会比BufferedWriter,更快BufferedWriter,因为它将执行更多的物理写入。

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

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