Joining paths in Java
Even though the original solution for getting the current directory using the empty String
works. But is recommended to use the user.dir
property for current directory and user.home
for home directory.
Path currentPath = Paths.get(System.getProperty("user.dir"));
Path filePath = Paths.get(currentPath.toString(), "data", "foo.txt");
System.out.println(filePath.toString());
output:
/Users/user/coding/data/foo.txt
From Java Path class Documentation:
A Path is considered to be an empty path if it consists solely of one name element that is
empty
. Accessing a file using anempty path is equivalent to accessing the default directory
of the file system.
Why Paths.get("").toAbsolutePath()
works
When an empty string is passed to the Paths.get("")
, the returned Path
object contains empty path. But when we call Path.toAbsolutePath()
, it checks whether path length is greater than zero, otherwise it uses user.dir
system property and return the current path.
Here is the code for Unix file system implementation: UnixPath.toAbsolutePath()
Basically you need to create the Path
instance again once you resolve the current directory path.
Also I would suggest using File.separatorChar
for platform independent code.
Path currentRelativePath = Paths.get("");
Path currentDir = currentRelativePath.toAbsolutePath(); // <-- Get the Path and use resolve on it.
String filename = "data" + File.separatorChar + "foo.txt";
Path filepath = currentDir.resolve(filename);
// "data/foo.txt"
System.out.println(filepath);
Output:
/Users/user/coding/data/foo.txt
Not an specific method.
If you use java 8 or better, you have 2 options:
a) Use java.util.StringJoiner
StringJoiner joiner = new StringJoiner(File.pathSeparator); //Separator
joiner.add("path1").add("path2");
String joinedString = joiner.toString();
b) Use String.join(File.pathSeparator, "path1", "path2");
If you use java 7 or lower, you may use commons-lang library from apache commons. The class StringUtils has a method to join strings using a separator.
c) StringUtils.join(new Object[] {"path1", "path2"}, File.pathSeparator);
A sidenote: You may use linux pathseparator "/" for windows (Just remember that absolute paths are something like "/C:/mydir1/mydir2". Using always "/" is very useful if you use protocols such as file://
Paths#get(String first, String... more)
states,
Converts a path string, or a sequence of strings that when joined form a path string, to a
Path
....
A
Path
representing an empty path is returned if first is the empty string and more does not contain any non-empty strings.
To get the current user directory you can simply use System.getProperty("user.dir")
.
Path path = Paths.get(System.getProperty("user.dir"), "abc.txt");
System.out.println(path);
Moreover, get
method uses variable length argument of String
, which will be used to provide subsequent path strings. So, to create Path
for /test/inside/abc.txt
you have to use it in a following way,
Path path = Paths.get("/test", "inside", "abc.txt");
You can do like
// /root
Path rootPath = Paths.get("/root");
// /root/temp
Path temPath = rootPath.resolve("temp");
A good detailed post is here Path Sample Usecase