2016-11-14 6 views
0
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); 
     } 

sun.nio.ch.FileChannelImpl.mapのスレッドでの例外 "メイン" java.nio.channels.NonWritableChannelException (FileChannelImpl.java:880)JAVA - 非書き込み可能チャネル例外

NonwritableChannelExceptionを取得上記のコードのために。助けてください。ありがとう!

+0

ur fileModeInとは何ですか? – developer

+0

私は入力としてfilemodeを取得していますが、デフォルトでREAD_WRITEとして設定しています – ShreyasMN

答えて

0

fileModeInFileChannel.MapMode.READ_WRITEが一致しなかった場合、小さな再生可能なプログラムがエラーになりました。

サンプルプログラム:

import java.io.*; 
import java.nio.channels.*; 
import java.nio.MappedByteBuffer; 
class SampleFileProcessor { 
    public static void main(String[] args) { 
     String fileName = args[0]; 
     String mode = args[1]; 
     JFP jf = new JFP(); 
     jf.FileProcessor(fileName,mode); 
    } 

    public void FileProcessor(String filenameIn, String fileModeIn){ 
     try { 
      RandomAccessFile randomaccessfile = new RandomAccessFile(filenameIn, fileModeIn); 
      FileChannel fileChannel = randomaccessfile.getChannel(); 
      MappedByteBuffer 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); 
     } 
    } 
} 

入力&出力:

echo "Non-matching fileModeIn and FileChannel.MapMode" 
java SampleFileProcessor input_file.txt r 
Exception in thread "main" java.nio.channels.NonWritableChannelException 
    at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:799) 
    at JFP.FileProcessor(JFP.java:19) 
    at JFP.main(JFP.java:9) 
<Error> 

echo "Matching fileModeIn and FileChannel.MapMode" 
java SampleFileProcessor input_file.txt rw 
<Success> 
0

ファイルをマッピングするときに、あなたがREAD_WRITEをしたい場合RandomAccessFileあなたは最終的に...

からそれを取得しているの作成時には、 "rw"を必要とします
関連する問題