Java:Javolution:如何使用UTF8ByteBufferWriter和MappedByteBuffer?

对于使用javolution的任何人,请指导我如何使用它。 任何代码段对我都有帮助。

这是我目前的代码:

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); } 

但它给了我一个例外:

位于abc.filedivision的abc.filedivision.FileMergeTest.mergeAllFilesJavolution2(FileMergeTest.java:100)的sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:716)中的线程“main”java.nio.channels.NonReadableChannelException中的exception。 FileMergeTest.main(FileMergeTest.java:27)

任何关于正确方向的指导都表示赞赏。

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

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

如果您尝试使用通道读取但不可读,则抛出此exception。

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