Java Paths.get () weird behavior in Linux

I am currently writing code in which I work a lot with files. I did all the processing of file paths (concatenation, normalization, etc.) Using Java classes nio Paths and Path. On Windows, everything works as expected, but on Linux, the behavior of the Paths class seems to be broken.

For example, the following code:

    System.out.println(File.separator);
    System.out.println(FileSystems.getDefault());
    Path path = Paths.get("../dir1/", "\\dir2\\file1").toAbsolutePath().normalize();
    System.out.println(path);
    if(path.toFile().exists()) {
        System.out.println(path + " exists");
    }

on Windows displays the following output:

\
sun.nio.fs.WindowsFileSystem
D:\projects\dir1\dir2\file1
true

but the same code on Linux Ubuntu 14.04 on both Java 1.7.0_79 (64 bit) and Java 1.8.0_60 (64 bit) leaves the path unnormalized:

/
sun.nio.fs.LinuxFileSystem
/home/semi/dir1/\dir2\file1

In addition, even if the file is on the path /home/semi/dir1/dir2/file1exists, it is reported as non-existent path.toFile().exists().

LinuxFileSystem.java WindowsFileSystem.java , /, \ ( WindowsPathParser.isSlash(char c)). Linux ?

​​ sun.nio.fs.LinuxFileSystem - ?

, , Linux ( ).

+4
1

Windows / , .

Linux . Paths.get() dir1 (1 ) \dir2\file1 ( 1 ).

+2

Source: https://habr.com/ru/post/1606370/


All Articles