The API is a great improvement over the old java.io.file
class, but an extension made me Kills in a strange form; Any method with the exception of the document that they can throw NoSuchFileException
, and even delete
say that it is optional.
I should be able to differentiate between failures due to missing files and other IO issues, but it seems that this is not guaranteed to be possible.
The option of calling files.exists ()
and as if the file is created between two tasks, if there is a risk of a race-state already.
Can I expect methods in files
NoSuchFileException
when appropriate? If so, where is it? If not, how can I find out the failure due to missing files?
Example: The method with Java 7.0.02 with Windows 7 is a NoSuchFileException
, although it is clearly documented by doing so is not:
Files.readAllLines (Paths.get ( "foo"), StandardCharsets.UTF_8)
java .nio.file.NoSuchFileException: sun.nio.fs.WindowsException.translateToIOException (WindowsException.java:79) foo. Java on sun.nio.fs.WindowsException.rethrowAsIOException (WindowsException: 97) sun.nio.fs.WindowsException. rethrowAsIOException (WindowsException.java:102) sun.nio.fs.WindowsFileSystemProvider.newByteChannel (WindowsFileSystemProvider.java:229) Java on (files java.nio.file.Files.newByteChannel: 315) java.nio.file.Files. N ewByteChannel (Files.java:361) on java.nio.file.spi.FileSystemProvider.newInputStream (FileSystemProvider.java:380) on java.nio.file.Files. On NewInputStream (files.java:106) on java.nio.file.Files.newBufferedReader java.nio.file.Files.readAllLines (Files.java:2660) (Files.java:2993)
Normally: No, you can use methods in the java.nio.file.files.files You can not trust
a NoSuchFileException
, but you can verify when it is expected.
As you can see from the Stacktrace, files
is used to perform the file operation FileSystemProvider
implementation is restricted (e.g.) and in return much to all the original (C) code example, I NoSuchFileException
to which depends on the operating system a ERROR_FILE_NOT_FOUND
or ERROR_PATH_NOT_FOUND for
Detected to Report. Another example is the newInputStream
route that is called the local Windows function.
Looking at the amount of code included for Windows only, I do not see how this example can be trusted for Linux for example.
In my experience, the Linux (POSX) file system behavior is very consistent, but the Windows (NT) file system is not behavioral: I do not consider Windows 7 to behave just like Windows 8.
Finally, a comment about the position of the race: No file system I do not guarantee that the files listed in the directory are actually present (some files have already been deleted, especially when one Are using multiple threads of operations on the files in the directory), such as files.exists ()
already in my experience, a bad idea unless you want to allocate many resources (Like file Make a connection to upload). When file is handled, it is better to believe that everything is in order and catch exceptions and then try to decide what is wrong. Like when reading a file, open it without checking whether the file exists or not, and if you catch an error, check whether the file exists or not can cause many I / O operations to be saved. Which in turn will help in performance.
No comments:
Post a Comment