【问题标题】:Java: Javolution: How to use UTF8ByteBufferWriter and MappedByteBuffer?Java:Javolution:如何使用 UTF8ByteBufferWriter 和 MappedByteBuffer?
【发布时间】:2011-09-17 02:21:17
【问题描述】:

对于任何使用 javolution 的人,请指导我如何使用它。 任何 sn-p 代码都对我有很大帮助。

这是我当前的代码:

public static void mergeAllFilesJavolution2()throws FileNotFoundException, IOException {
    String fileDir = "C:\\TestData\\w12";
    File dirSrc = new File(fileDir);
    File[] list = dirSrc.listFiles();
    long start = System.currentTimeMillis();

    String outFile = fileDir + "\\..\\merged.txt";
    File file2 = new File(outFile);
    //file2.createNewFile();
    FileChannel fc2 = (new RandomAccessFile(file2, "rw")).getChannel(); 

    for(int j=0; j<list.length; j++){
        int chr;
        String srcFile = list[j].getPath();

        File file = new File(srcFile);
        FileChannel fc = (new FileInputStream(file)).getChannel(); 
        MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, 0, file.length());
        UTF8ByteBufferReader inFile= new UTF8ByteBufferReader().setInput(buf);

        MappedByteBuffer buf2 = fc2.map(MapMode.READ_WRITE, file2.length(), file.length());
        UTF8ByteBufferWriter outPut= new UTF8ByteBufferWriter().setOutput(buf2);

        while((chr=inFile.read()) != -1) {
            outPut.write(chr);
        }
        outPut.close();
        inFile.close();
    }
    System.out.println(System.currentTimeMillis()-start);
}

但它给了我一个例外:

线程“main”中的异常 java.nio.channels.NonReadableChannelException 在 sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:716) 在 abc.filedivision.FileMergeTest.mergeAllFilesJavolution2(FileMergeTest.java:100) 在 abc.filedivision.FileMergeTest.main(FileMergeTest.java:27)

感谢任何有关正确方向的指导。

【问题讨论】:

  • 值得注意的是,该错误与 Javolution 无关。
  • @Peter Lawrey,你有关于如何使用 javolution 的 UTF8ByteBufferWriter 类的 sn-p 代码吗?
  • 现在,我认为这是有史以来最快的文件合并。感谢彼得劳里

标签: java javolution


【解决方案1】:

我的猜测是它在这里抱怨(这就是为什么查看有错误的代码行很重要)

FileChannel fc2 = (new FileOutputStream(file2, true)).getChannel(); 
MappedByteBuffer buf2 = fc2.map(MapMode.READ_WRITE, 0, file2.length());

如果您尝试使用通道读取但不可读,则会引发此异常。

如果您在“rw”、“rws”或“rwd”模式下使用 RandomAccessFile,则只能以 READ_WRITE 模式打开通道

【讨论】:

  • 我编辑了原始帖子以应用他的解决方案。并更正某些部分,因为它会覆盖自己。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多