Search code examples
javanio

How to create a file Path from a folder Path


I am probably overlooking something but what's the right way to create a file Path from a folder Path? This is what I'm doing but it seems wrong to convert the folder path to a string just to reconstruct it. Is there a better way?

Path testFolder = Files.createTempDirectory("fileFinder");
Path testFile = Paths.get(testFolder.toString(), "sample.java");
Files.createFile(testFile);

Solution

  • Path class has resolve() method to join two paths together. It is overloaded to take a String as a parameter (other path).

    So your expression to produce the combined path will be:

    testfolder.resolve("sample.java")