an issue that took me some time to solve when facing that problem while coding.
In short, java designers says that it is more safe and secure to use the "/" (forward slash) as a path separator in any case: when coding for a Windows machine or for any other machine.
and this is what i found in our legendary KS && BB guide:
"Remember that we are using a file system–specific PathMatcher . This means
slashes and backslashes can be treated differently, depending on what
operating system you happen to be running. The previous example does print
the same output on both Windows and UNIX because it uses forward slashes.
However, if you change just one line of code, the output changes:
Path path = Paths.get("com\\java\\One.java");
Now Windows still prints:
false
true
false
true
However, UNIX prints:
true
false
true
true
Why? Because UNIX doesn't see the backslash as a directory boundary. The
lesson here is to use / instead of \\ so your code behaves more predictably
across operating systems."
(KS && BB guide OCA/OCP Java SE 7 study guide p. 520)
The other question i would like to ask is what can we coders actually do with the result that we get from the relativize() method as a Path object? can we actually use it as a part of another Path object in order to create a new file?
Lets say for example can we code something like the following:
Code: Select all
Path p1 = Paths.get("/home/Paul/Documents/etsStudents");
Path p2 = Paths.get("/home/Paul/Documents/etsStudents/OCPexamJavaSE7/NIO/RelativizeAPI");
Path p3 = p1.relativize(p2);
// And now lets try to create a useful path object from p3:
Path p4 = Paths.get(p3.toString(), "Relativize.txt");
// Now for the actual creation of the file:
Files.createFile(p4);
