简体   繁体   中英

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);
Path testFile = Files.createFile(Files.createTempDirectory("fileFinder").resolve("sample.java"));

但是也许您只需要一个临时文件:

Path testFile = Files.createTempFile("fileFinder");

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM