How to combine paths in Java?

JavaPath

Java Problem Overview


Is there a Java equivalent for System.IO.Path.Combine() in C#/.NET? Or any code to accomplish this?

This static method combines one or more strings into a path.

Java Solutions


Solution 1 - Java

Rather than keeping everything string-based, you should use a class which is designed to represent a file system path.

If you're using Java 7 or Java 8, you should strongly consider using java.nio.file.Path; Path.resolve can be used to combine one path with another, or with a string. The Paths helper class is useful too. For example:

Path path = Paths.get("foo", "bar", "baz.txt");

If you need to cater for pre-Java-7 environments, you can use java.io.File, like this:

File baseDirectory = new File("foo");
File subDirectory = new File(baseDirectory, "bar");
File fileInDirectory = new File(subDirectory, "baz.txt");

If you want it back as a string later, you can call getPath(). Indeed, if you really wanted to mimic Path.Combine, you could just write something like:

public static String combine(String path1, String path2)
{
    File file1 = new File(path1);
    File file2 = new File(file1, path2);
    return file2.getPath();
}

Solution 2 - Java

In Java 7, you should use resolve:

Path newPath = path.resolve(childPath);

While the NIO2 Path class may seem a bit redundant to File with an unnecessarily different API, it is in fact subtly more elegant and robust.

Note that Paths.get() (as suggested by someone else) doesn't have an overload taking a Path, and doing Paths.get(path.toString(), childPath) is NOT the same thing as resolve(). From the Paths.get() docs:

> Note that while this method is very convenient, using it will imply an assumed reference to the default FileSystem and limit the utility of the calling code. Hence it should not be used in library code intended for flexible reuse. A more flexible alternative is to use an existing Path instance as an anchor, such as: > Path dir = ... Path path = dir.resolve("file");

The sister function to resolve is the excellent relativize:

Path childPath = path.relativize(newPath);

Solution 3 - Java

The main answer is to use File objects. However http://commons.apache.org/io/">Commons IO does have a class http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FilenameUtils.html">FilenameUtils</a> that can do this kind of thing, such as the concat() method.

Solution 4 - Java

platform independent approach (uses File.separator, ie will works depends on operation system where code is running:

java.nio.file.Paths.get(".", "path", "to", "file.txt")
// relative unix path: ./path/to/file.txt
// relative windows path: .\path\to\filee.txt

java.nio.file.Paths.get("/", "path", "to", "file.txt")
// absolute unix path: /path/to/filee.txt
// windows network drive path: \\path\to\file.txt

java.nio.file.Paths.get("C:", "path", "to", "file.txt")
// absolute windows path: C:\path\to\file.txt

Solution 5 - Java

I know its a long time since Jon's original answer, but I had a similar requirement to the OP.

By way of extending Jon's solution I came up with the following, which will take one or more path segments takes as many path segments that you can throw at it.

Usage

Path.combine("/Users/beardtwizzle/");
Path.combine("/", "Users", "beardtwizzle");
Path.combine(new String[] { "/", "Users", "beardtwizzle", "arrayUsage" });

Code here for others with a similar problem

public class Path {
    public static String combine(String... paths)
    {
        File file = new File(paths[0]);
    
        for (int i = 1; i < paths.length ; i++) {
            file = new File(file, paths[i]);
        }
    
        return file.getPath();
    }
}

Solution 6 - Java

To enhance JodaStephen's answer, Apache Commons IO has FilenameUtils which does this. Example (on Linux):

assert org.apache.commons.io.FilenameUtils.concat("/home/bob", "work\\stuff.log") == "/home/bob/work/stuff.log"

It's platform independent and will produce whatever separators your system needs.

Solution 7 - Java

Late to the party perhaps, but I wanted to share my take on this. I'm using a Builder pattern and allow conveniently chained append(more) calls, and allows mixing File and String. It can easily be extended to support working with Path objects as well, and furthermore handles the different path separators correctly on both Linux, Macintosh, etc.

public class Files 	{
	public static class PathBuilder {
	    private File file;

	    private PathBuilder ( File root ) {
	        file = root;
	    }

	    private PathBuilder ( String root ) {
	        file = new File(root);
	    }

	    public PathBuilder append ( File more ) {
	        file = new File(file, more.getPath()) );
            return this;
	    }

	    public PathBuilder append ( String more ) {
            file = new File(file, more);
            return this;
	    }

	    public File buildFile () {
	        return file;
	    }
	}

	public static PathBuilder buildPath ( File root ) {
	    return new PathBuilder(root);
	}

	public static PathBuilder buildPath ( String root ) {
	    return new PathBuilder(root);
	}
}

Example of usage:

File root = File.listRoots()[0];
String hello = "hello";
String world = "world";
String filename = "warez.lha"; 

File file = Files.buildPath(root).append(hello).append(world)
              .append(filename).buildFile();
String absolute = file.getAbsolutePath();

The resulting absolute will contain something like:

/hello/world/warez.lha

or maybe even:

A:\hello\world\warez.lha

Solution 8 - Java

If you do not need more than strings, you can use com.google.common.io.Files

Files.simplifyPath("some/prefix/with//extra///slashes" + "file//name")

to get

"some/prefix/with/extra/slashes/file/name"

Solution 9 - Java

Here's a solution which handles multiple path parts and edge conditions:

public static String combinePaths(String ... paths)
{
  if ( paths.length == 0)
  {
    return "";
  }
  
  File combined = new File(paths[0]);
  
  int i = 1;
  while ( i < paths.length)
  {
    combined = new File(combined, paths[i]);
    ++i;
  }
  
  return combined.getPath();
}

Solution 10 - Java

This also works in Java 8 :

Path file = Paths.get("Some path");
file = Paths.get(file + "Some other path");

Solution 11 - Java

This solution offers an interface for joining path fragments from a String[] array. It uses java.io.File.File(String parent, String child):

    public static joinPaths(String[] fragments) {
        String emptyPath = "";
        return buildPath(emptyPath, fragments);
    }

    private static buildPath(String path, String[] fragments) {
		if (path == null || path.isEmpty()) {
			path = "";
        }
        
        if (fragments == null || fragments.length == 0) {
            return "";
        }

		int pathCurrentSize = path.split("/").length;
		int fragmentsLen = fragments.length;

		if (pathCurrentSize <= fragmentsLen) {
			String newPath = new File(path, fragments[pathCurrentSize - 1]).toString();
			path = buildPath(newPath, fragments);
		}

        return path;
	}

Then you can just do:

String[] fragments = {"dir", "anotherDir/", "/filename.txt"};
String path = joinPaths(fragments);

Returns:

"/dir/anotherDir/filename.txt"

Solution 12 - Java

Assuming all given paths are absolute paths. you can follow below snippets to merge these paths.

String baseURL = "\\\\host\\testdir\\";
String absoluteFilePath = "\\\\host\\testdir\\Test.txt";;
String mergedPath = Paths.get(baseURL, absoluteFilePath.replaceAll(Matcher.quoteReplacement(baseURL), "")).toString();

output path is \\host\testdir\Test.txt.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnovicerView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaAleksandr DubinskyView Answer on Stackoverflow
Solution 3 - JavaJodaStephenView Answer on Stackoverflow
Solution 4 - JavaMaksim KostrominView Answer on Stackoverflow
Solution 5 - JavaisNaN1247View Answer on Stackoverflow
Solution 6 - JavaalpianView Answer on Stackoverflow
Solution 7 - JavaStephan HenningsenView Answer on Stackoverflow
Solution 8 - JavaGismo RanasView Answer on Stackoverflow
Solution 9 - JavaBillView Answer on Stackoverflow
Solution 10 - JavarootExplorrView Answer on Stackoverflow
Solution 11 - JavaJackCidView Answer on Stackoverflow
Solution 12 - JavaU_R_Naveen UR_NaveenView Answer on Stackoverflow