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"
Related
I have a path like this:
/foo/bar/... (can contain other subdirs or files)
I want to add a "middle" folder in the mid of said path, leading to:
/foo/middle/bar/...
What's an elegant way to do that?
Yeah so what you can do is have a path like the following that represents the absolute path of foo/bar/
Path path = Paths.get("foo", "bar");
Then you can get the parent of the current path.
Path parent = path.getParent();
Then you can resolve a sibling, a new path.
Path newChild = parent.resolve("middle");
Then you cna create that path if it doesnt exist.
Files.createDirectory(newChild);
Not sure what you have, or what you want, but assuming your path is a String
def path = "/foo/bar/baz/whee/yay"
You could just split the string on /
def segments = path.split('/')
insert one at position 2 (split has left us an empty entry before the initial /)
def inserted = segments[0..1] + 'new' + segments[2..-1]
Then join them back together
assert inserted.join('/') == '/foo/new/bar/baz/whee/yay'
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 ?
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);
I am probably overlooking something but what's the right way to create a file Path from a folder Path? This is what I'm doing but it seems wrong to convert the folder path to a string just to reconstruct it. Is there a better way?
Path testFolder = Files.createTempDirectory("fileFinder");
Path testFile = Paths.get(testFolder.toString(), "sample.java");
Files.createFile(testFile);
Path testFile = Files.createFile(Files.createTempDirectory("fileFinder").resolve("sample.java"));
But maybe all you need is just one tempfile:
Path testFile = Files.createTempFile("fileFinder");
Path class has resolve() method to join two paths together. It is overloaded to take a String as a parameter (other path).
So your expression to produce the combined path will be:
testfolder.resolve("sample.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.