I use the Path interface in my code, but I understand that there is a specificity when working with UNIX and Windows, but I just can’t understand if this is limited by the beginning in absolute paths and backslashes with screening or not? In general, how do I use Path objects so that it works the same everywhere.
1 answer
Path has several implementations for specific file systems.
The format of the file path entry is different, and the fact that it will work in windows will not necessarily work in linux or some other OS. To smooth this problem as much as possible, a way to specify paths is registered in javadoc .
Path Path = FileSystems.getDefault (). GetPath ("logs", "access.log");
BufferedReader reader = Files.newBufferedReader (path, StandardCharsets.UTF_8);
There is also a way to use a separator, which depends on the current file system.
String path = "dir" + File.pathSeparator + "file.txt"; - Yes, I also saw this example in the docks Path Path = FileSystems.getDefault (). GetPath ("logs", "access.log"); but did not understand the syntax of the arguments. So it seems clear that logs is a folder, and access.log is the name of a file. But in the end, what kind of separator to use if I have ("/a/b/c/logs","/a/b/c/logs/access.log") ??? How to apply this case is not clear ??? - Pavel
- oneThis is a trick that you can pass an arbitrary number of lines, which will be separated by a symbol. And this symbol is already determined depending on the file system. - Artem Konovalov
- oneIn your example there will be something like .getPath ("a", "b", "c", "logs", "access.log") - Artem Konovalov
|