2016-11-14 2 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); 
     } 

Exception in thread "main" java.nio.channels.NonWritableChannelException bei sun.nio.ch.FileChannelImpl.map (FileChannelImpl.java:880)JAVA - Nicht beschreibbar Kanal Ausnahme

gibt es die NonwritableChannelException für den obigen Code. Bitte helfen Sie. Vielen Dank.!!

+0

Was ist Ihr DateiModusIn? – developer

+0

Ich erhalte einen Dateimodus als Eingabe, setze aber standardmäßig READ_WRITE – ShreyasMN

Antwort

0

Das kleine reproduzierbare Programm an meinem Ende traf Ihren Fehler, wenn die fileModeIn und FileChannel.MapMode.READ_WRITE nicht übereinstimmten.

Beispielprogramm:

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

Eingang & Ausgang:

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

Wenn Sie READ_WRITE wollen, wenn die Dateizuordnung Sie "rw" müssen, wenn die Schaffung RandomAccessFile Sie schließlich bekommen es aus ..

Verwandte Themen