I would like to have a directory path that is A/%Name%/B, where %Name% is a string I declared earlier, is there a Path.Combine like in C#? Or what could I use?
Asked
Active
Viewed 68 times
-2
-
So far, (since I am relatively new to Java) I have tried nothing. I don't know where to start. – Zac Heimgartner Mar 07 '13 at 00:31
-
Take a look at this post. http://stackoverflow.com/questions/412380/combine-paths-in-java – Breavyn Mar 07 '13 at 00:31
3 Answers
1
If I understand it correctly , you are trying to format a String.
You can use
String directoryName = "test";
String path = "A/%s/B";
String.format(path,directory);
or something like below based on your requirement
File f = new File(String.format(path,directory));

Avinash Singh
- 3,421
- 2
- 20
- 21
1
Use the File
constructor:
File combined = new File(new File("A", name), "B");
You could even write a convenience method to do that if you wanted:
public static File combine(String base, String... sections)
{
File file = new File(base);
for (String section : sections) {
file = new File(file, section);
}
return file;
}
Then you can call it as:
File x = combine("A", name, "B");
Note that using the File
constructor like this is generally considered preferable to assuming a directory separator of /
, even though in practice that works on all platforms that I'm aware of.
1
You can use:
String yourString = ...;
File theFile = new File("A/" + yourString + "/B");

João Vitor Verona Biazibetti
- 2,223
- 3
- 18
- 30