"foo/bar" I am trying to achieve th...">

Joining paths in Java

In Python I can join two paths using os.path.join :

 os.path.join("foo", "bar") # => "foo/bar" 

I am trying to achieve the same thing in Java without worrying if OS Unix , Solaris or Windows :

 public static void main(String[] args) { Path currentRelativePath = Paths.get(""); String current_dir = currentRelativePath.toAbsolutePath().toString(); String filename = "data/foo.txt"; Path filepath = currentRelativePath.resolve(filename); // "data/foo.txt" System.out.println(filepath); } 

I expected Path.resolve( ) join my current directory /home/user/test with data/foo.txt by doing /home/user/test/data/foo.txt . What am I wrong?

+67
java
Dec 25 '15 at 2:16
source share
5 answers

Although the original solution to get the current directory using empty String works. But it is recommended to use the user.dir property for the current directory and user.home for the home directory.

 Path currentPath = Paths.get(System.getProperty("user.dir")); Path filePath = Paths.get(currentPath.toString(), "data", "foo.txt"); System.out.println(filePath.toString()); 

exit:

 /Users/user/coding/data/foo.txt 



From the Java Path class Documentation:

A path is considered empty if it consists of only one name element, which is empty . Accessing a file using empty path is equivalent to accessing the default directory file system.




Why Paths.get("").toAbsolutePath() works

When an empty string is passed to Paths.get("") , the returned Path object contains an empty path. But when we call Path.toAbsolutePath() , it checks to see if the path is longer than zero, otherwise it uses the user.dir system property and returns the current path.

Here is the code for implementing the Unix file system: UnixPath.toAbsolutePath ()




Basically, you need to instantiate the Path again as soon as you resolve the current directory path.

I would also 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 
+59
Dec 25 '15 at 2:25
source share

Paths#get(String first, String... more) states,

Converts a path string or sequence of strings that, when combined, form a path string into Path .

...

A Path representing an empty path is returned if the empty string and much 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); 

In addition, the get method uses a variable-length argument String , which will be used to provide subsequent path lines. So, to create a Path for /test/inside/abc.txt , you should use it like this,

 Path path = Paths.get("/test", "inside", "abc.txt"); 
+14
Dec 25 '15 at 2:34
source share

Undefined method.

If you use java 8 or higher, 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 can use the commons-lang library from the apache community. The StringUtils class has a method for concatenating strings using a separator.

c) StringUtils.join(new Object[] {"path1", "path2"}, File.pathSeparator);

A sidenote: you can use the linux pathseparator "/" for Windows (just remember that absolute paths are something like "/ C: / mydir1 / mydir2". Using "/" is always very useful if you use such protocols, as file: //

+7
Dec 25 '15 at 2:34
source share

The easiest way:

 Path filepath = Paths.get("foo", "bar"); 

You should not write Paths.get("") . I am surprised that it works at all. If you want to explicitly reference the current directory, use Paths.get(System.getProperty("user.dir")) . If you want to use the user's home directory, use Paths.get(System.getProperty("user.home")) .

You can also combine approaches:

 Path filepath = Paths.get( System.getProperty("user.home"), "data", "foo.txt"); 
+5
Dec 25 '15 at
source share

The most reliable, platform-independent way to combine paths in Java is to use Path::resolve (as stated in the JavaDoc for Paths::get ). For an array of arbitrary length of strings representing pieces of the path, they can be combined together using a Java stream:

 private static final String[] pieces = { System.getProperty("user.dir"), "data", "foo.txt"}; public static void main (String[] args) { Path dest = Arrays.stream(pieces).reduce( /* identity */ Paths.get(""), /* accumulator */ Path::resolve, /* combiner */ Path::resolve); System.out.println(dest); } 
+1
Nov 01 '16 at 6:05
source share



All Articles