2014-10-14 6 views
7

Unter OSX mit JVM 7, ich sehe, dass FileChannel.open mit CREATE_NEW nicht mit der docs übereinstimmen scheint. Der Code unten, würde ich erwarten, eine neue Datei zu erstellen, und nur fehlschlagen, wenn es nicht möglich ist (Berechtigungen, Datenträgerproblem) oder dass die Datei bereits vorhanden ist.Unter OSX und JVM 7 scheint FileChannel.open defekt

scala> FileChannel.open(new File("/tmp/doesnotexist").toPath, StandardOpenOption.CREATE_NEW) 
java.nio.file.NoSuchFileException: /tmp/doesnotexist 
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86) 
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) 
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) 
    at sun.nio.fs.UnixFileSystemProvider.newFileChannel(UnixFileSystemProvider.java:177) 
    at java.nio.channels.FileChannel.open(FileChannel.java:287) 
    at java.nio.channels.FileChannel.open(FileChannel.java:334) 
    ... 32 elided 

scala> val path = new File("/tmp/doesnotexist") 
path: java.io.File = /tmp/doesnotexist 

scala> path.createNewFile() 
res9: Boolean = true 

scala> FileChannel.open(path.toPath, StandardOpenOption.CREATE_NEW) 
res10: java.nio.channels.FileChannel = [email protected] 

scala> FileChannel.open(path.toPath, StandardOpenOption.CREATE_NEW) 
res11: java.nio.channels.FileChannel = [email protected] 

scala> FileChannel.open(path.toPath, StandardOpenOption.CREATE_NEW) 
res12: java.nio.channels.FileChannel = [email protected] 

Hier ist die Java, die ich

$ java -version 
java version "1.7.0_55" 
Java(TM) SE Runtime Environment (build 1.7.0_55-b13) 
Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode) 

bin mit Ist das ein Dokument (oder die Auslegung doc) Problem oder einen Fehler auf OSX (vielleicht sogar Linux? Noch nicht getestet)?

Antwort

10

Sie müssen WRITE zusammen mit CREATE_NEW angeben. Ich habe gerade dies auf meinem OS X für Sie getestet, und es funktioniert wie erwartet:

FileChannel.open(Paths.get("/tmp/doesnotexist"), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); 
+0

Danke, das hat funktioniert! – ekaqu