Imagine I have a ‘base’ path object, denoting a directory, and a ‘relative’ path object denoting some file within the base.
I would expect that code to look somewhat like
AbsolutePath base = new AbsolutePath("/tmp/adirectory");
RelativePath relativeFilePath = new RelativePath("filex.txt");
AbsolutePath absoluteFile = base.append( relativeFilePath );
But in the Java API (which I don’t yet know very well) I find only File, with which I can do nothing better than
File base = new File("/tmp/adirectory");
File relativeFilePath = new File("filex.txt");
File absoluteFile = base.toString()
+ File.separator
+ relativeFilePath.toString();
Is there a better way?
The closest you can get with
java.io.Fileis theFile(File, String)constructor:If you can use the
Pathclass introduced in Java 7, then you can use theresolve()method, which does exactly what you want:Please note that if
baseis not an absolute path, thencombinedwon’t be absolute either! If you need an absolute path, then for aFileyou’d usegetAbsoluteFile()and for aPathyou’d usetoAbsoutePath().