Relative path, how exactly work? - java

I have the following code:
//on WINDOWS:
Path path = Paths.get("photos\\vacation"); // windows
Path path1 = Paths.get("yellowstone");
Path path2 = path.relativize(path1);
System.out.print(path2);
output: ../yellowstone
//on LINUX
Path path = Paths.get("photos/vacation"); // unix
Path path1 = Paths.get("yellowstone");
Path path2 = path.relativize(path1);
System.out.print(path2);
output: ../../yellowstone
Why do I get two different relative paths?
The official javadoc mentions only that if both paths have a root component (which is not the case) the result will be system dependent.
Is there a different rule for windows?
Thanks in advance.

Related

`java.nio.file.Path`- necessary to `path = Files.createDirectories(path)`?

Talking about the Java java.nio.file.Path and associated utilities.
As the title suggests, is it really necessary for one to set the path they just created?
I.E,
Path path = Path.of("some", "dir");
path = Files.createDirectories(path);
//continue to use path variable
vs
Path path = Path.of("some", "dir");
Files.createDirectories(path);
//continue to use path variable
Is the second example adequate, or are there actually changes made that are relevant/ required that are returned in the Path object in createDirectories?

Behavior of Path.toRealPath() in Java with more than one level of symbolic link

what is the behavior of:
Path.toRealPath()
when there is more than one level of symbolic links? I am debugging a program remotely for a friend who is on Linux, and I suspect that he has more than one symbolic links levels.
I am currently doing:
Path path = Path.get(uri);
if (Files.isSymbolicLink(path)) {
path = path.toRealPath();
uri = path.toURI();
}
But maybe I should do something like that:
Path path = Path.get(uri);
while (Files.isSymbolicLink(path)) {
path = path.toRealPath();
}
uri = path.toURI();

Java - create relative java.nio.Path from two java.nio.Path's

I have a java.nio.Path which points to an absolute path:
/home/user/project/resources/configuration.xml
I have a second java.nio.Path which points to the root directory of the project, also an absolute path:
/home/user/project
Is it now possible to create a java.nio.Path which holds the relative path between the two:
resources/configuration.xml
This is precisely what the relativize(Path) method does:
Path confFile = Paths.get("/home/user/project/resources/configuration.xml");
Path rootDir = Paths.get("/home/user/project");
Path relative = rootDir.relativize(confFile);

Getting relativePath and then diving deeper

I have a package structure like
A
|_B
|_C
|_D
|_"myFile.txt"
myMain.java
In my main i want to do something like
Path currentRelativePath = Paths.get("");
Which gives me the current relative path to where my Main() is located. Something along the lines of /home/myprojects/project1/src/ How do I amend the above to get me the relative path all the way down to the level of myFile.txt?
I have tried things like:
Path currentRelativePath = Paths.get("" + "/B/C/D/");
and
Path currentRelativePath = Paths.get("","B/C/D/");
and
Path currentRelativePath = Paths.get("");
Path finalPath = Paths.get(currentRelativePath.toString(),"/B/C/D/");
But in each case it only gets the "/B/C/D/" portion and not the beginning of the path.
//Gets from the current working directory a path to B/C/D
Path path = Paths.get("B/C/D");
//String equal to "B/C/D"
path.toString();
//String equal to "/home/myprojects/project1/src/B/C/D"
path.toAbsolutePath().toString();
I think you are confusing relative and absolute paths. /home/myprojects/project1/src/ is an absolute path, not as you say in your question "the current relative path"

Get absolute path in Java

I have string:
String s = "~/abc/d.png"
How can I get absolute path of this file?
I've tried:
File file = new File(s);
String absolutePath = file.getAbsolutePath();
But it can't reslove ~ symbol.
Java isn't going to know what the ~ means since it's a shell expansion for your home directory. You can do this before handing it off to File:
s = s.replace("~",System.getProperty("user.home"));

Categories

Resources