-2

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?

3 Answers3

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.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

You can use:

String yourString = ...;
File theFile = new File("A/" + yourString + "/B");