2016-11-14 1 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)자바 - 비 쓰기 가능한 채널 예외

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

입력 & 출력 : 파일을 매핑 할 때 READ_WRITE를 원하는 경우 RandomAccessFile 당신이 궁극적에서을 받고 만들 때

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

당신이 "rw" 필요 ..