2014-10-14 13 views
7

Su OSX con JVM 7, vedo che FileChannel.open con CREATE_NEW non sembra essere conforme allo docs. Il codice qui sotto, mi aspetterei di creare un nuovo file, e fallirebbe solo se non fosse possibile (autorizzazioni, problema del disco) o che il file esistesse già.Su OSX e JVM 7, FileChannel.open sembra essere rotto

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] 

Ecco la Java che sto usando

$ 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) 

Si tratta di un documento (o l'interpretazione del doc) problema, o di un bug su OSX (forse anche Linux? Non ancora testato)?

risposta

10

È necessario specificare WRITE insieme a CREATE_NEW. Ho appena testato questo sul mio OS X per te, e funziona come previsto:

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

Grazie, ha funzionato! – ekaqu