0

Is there an easy way to append to a path created like this:

 final Path path = Files.createTempDirectory(...);

Suppose this creates a temp dir in /tmp/xyx_123/. Now I want path to create another folder under /tmp/xyz_123/ called foo something like

 path.createDirectory("foo"); or path.appendDirectory("foo");

Is there an easy way to do this?

user2399453
  • 2,930
  • 5
  • 33
  • 60
  • [Path operations](https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html) might be a good place to start, [this](http://stackoverflow.com/questions/711993/does-java-have-a-path-joining-method) and [this](http://stackoverflow.com/questions/412380/combine-paths-in-java) might also be useful – MadProgrammer Jun 10 '16 at 21:59

2 Answers2

2

You can do this:

Path path = Files.createTempDirectory("xyx_123");
File fPath = path.toFile();
File addedDir = new File(fPath, "foo");
addedDir.mkdir();
Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
1

Straight from Path Operations, Joing two paths

// Solaris
Path p1 = Paths.get("/home/joe/foo");
// Result is /home/joe/foo/bar
System.out.format("%s%n", p1.resolve("bar"));

or

// Microsoft Windows
Path p1 = Paths.get("C:\\home\\joe\\foo");
// Result is C:\home\joe\foo\bar
System.out.format("%s%n", p1.resolve("bar"));
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366