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

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);

Related

NIO2 Path Symbols Java

If I created a two paths such as:
Path path3 = Paths.get("E:\\data");
Path path4 = Paths.get("E:\\user\\home");
And then make a new Path(relativePath) by using the relativize() method on the two paths, creating: "..\user\home" does the path symbol(..) in this case refer to "data" or does it just indicate a relative path?
Path relativePath = path3.relativize(path4);
// ..\user\home <- output
So my Question is, what does the Path symbol (..) represent?
The relativize method needs two inputs but doesn't "secretly" encode the base path into it's output, so your relativePath has to be applied to another base path to actually access a path on disk.
But you can apply it to a different base path, e.g. if you want to sync two folder structures below two different base paths.
tl;dr: it just indicates a relative path.
But take care with your path separator: if you hardcode that into your path strings like in your example, it will fail on other systems. Better split up the individual parts in extra strings like this:
Path path4 = Paths.get("E:", "user", "home");

Selenium - Java: How to get absolute path to the file

I created the method where I get an absolute path but when I debug I get an incorrect path to the file which should be uploaded.
So, the method where I get absolute path:
public String getFilePathByFormat(String filePath) {
File file = new File(filePath);
return file.getAbsolutePath();
}
Then I use this method in the general low-level method for uploading:
public void uploadFile(WebElement webElement, String filePath){
try {
webDriver.manage().timeouts().implicitlyWait(40, SECONDS);
webElement.sendKeys(getFilePathByFormat(filePath));
}catch (Exception e){
printErrorAndStopTest();
}
}
And when I debug and evaluate incorrect path gets:
E:\acceptance-tests\src\test\resources, BUT after disk name, one more folder should be - where the project located.
What's wrong and why getAbsolutePath doesn't build the correct path?
Thanks
There are two types of file path in file system.
1) An absolute path always starts from root element and contains complete directory list required to locate the file. For example, '/Users/username/filename.txt' on Unix systems or 'C:\Users\username\filename.txt' on Windows systems.
A relative path does not have any directory listing and needs to be combined with another path in order to access a file. For example, username/filename.txt is a relative path; Note that it does not have any forward or backward slashes at the beginning.
getAbsolutePath() returns the absolute path of a file and works like below.
File object is created with absolute pathname - This method simply returns the pathname provided to create the file. And in case of Windows System, drive name is appended at beginning by default if it is not present in absolute path name given.
File object is created using relative path - Here relative path name is made absolute by resolving it against the current user directory.
In this case, absolute path '/acceptance-tests/src/test/resources/test4.pdf' is passed; As mentioned for windows system, drive details are prefixed with given path and returned as absolute path.
To make it work, you can pass the relative path of file 'src/test/resources/test4.pdf' or just pass the file name 'test4.pdf'.

Java: Why Absolute path missing 'src' folder

I'm passing relative path of My.class and when looking for 'dir.getAbsolutePath', it omits 'src' folder
String absuliteClassPath = "com/test/My.class"
File dir = new File(cls.substring(0, cls.lastIndexOf("/")));
System.out.println(dir.getAbsolutePath())
It prints
/Users/swapnil.kotwal/Swapnil/myproject/com/test
But is should print
/Users/swapnil.kotwal/Swapnil/myproject/src/com/test
Any Idea why ?

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"

Why does Path class allow file within a file?

I'm playing around with the NIO Path stuff, and came across this quesion:
What will the following code fragment print?
Path p1 = Paths.get("\\personal\\readme.txt");
Path p2 = Paths.get("\\index.html");
Path p3 = p1.relativize(p2);
System.out.println(p3);
The answer is
..\..\index.html
But this would make the entire Path:
\personal\readme.txt\index.html
This looks like nonsense to me, as you can't put a file within a file like this. Can you?
If readme.txt were a directory instead of a file, I would be perfectly OK with this, but I'm very confused as to why it allows a filepath like this to exist?
Or is there some weird way that you can actually do this?
Both ISOs and Zip files (therefore JAR/WAR/XUL/CHM...) represent files that can contain a folder structure of files. These files can be handled either as a file, or as a folder; both are legitimate uses for them. Therefore, this would be semantically meaningful:
Path p1 = Paths.get("\\personal\\photos.zip");
Path p2 = Paths.get("\\family\\me.png");
Path p3 = p1.relativize(p2);
System.out.println(p3);"
While I am not aware of any implementations in Java that behave this way, it is a semantic used in XUL and Windows Explorer.
The result is ..\..\index.html
From the javadoc:
This method attempts to construct a relative path that when resolved
against this path, yields a path that locates the same file as the
given path. For example, on UNIX, if this path is "/a/b" and the given
path is "/a/b/c/d" then the resulting relative path would be "c/d".
Where this path and the given path do not have a root component, then
a relative path can be constructed.
This means that you would have to go up two folders from this path to reach a path from which you can reach your given path, index.html.
Remember, these are all paths, not actual files/file descriptors.
Path p1 = Paths.get("\\personal\\readme.txt");
Path p2 = Paths.get("\\personal\\index.html");
Path p3 = p1.relativize(p2);
System.out.println(p3);
prints ..\index.html, meaning from \\personal\\readme.txt, go up one and then access index.html.

Categories

Resources