簡體   English   中英

如何從文件夾路徑創建文件路徑

[英]How to create a file Path from a folder Path

我可能會忽略某些東西,但是從文件夾Path創建文件Path的正確方法是什么? 這就是我正在做的事情,但是將文件夾路徑轉換為字符串以重建它似乎是錯誤的。 有沒有更好的辦法?

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類具有resolve()方法以將兩個路徑連接在一起。 以String作為參數( 其他路徑 )是重載的。

因此,產生組合路徑的表達式將是:

testfolder.resolve("sample.java")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM