public FileProcessor(String filenameIn, String fileModeIn){
try {
     randomaccessfile = new RandomAccessFile(filenameIn, fileModeIn);
     fileChannel = randomaccessfile.getChannel();
     buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileChannel.size());
} catch (FileNotFoundException e) {
    System.err.println("Error while creating a File");
    e.printStackTrace();
    System.exit(1);
} catch (IOException e) {
    System.err.println("Error while creating MappedByteBuffer");
    e.printStackTrace();
    System.exit(1);
}


得到

Exception in thread "main" java.nio.channels.NonWritableChannelException
at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:880)

最佳答案

如果要在映射文件时使用READ_WRITE,则在创建"rw"时需要RandomAccessFile,则最终从中获取文件。

10-08 17:43