获取我当前正在使用的目录的子目录
Paths.get(currentpath.toString(), "subdirectory");
有没有更好的方法?我特别不喜欢toString调用,.toFile()
如果可能的话,我宁愿不使用。
我尝试在这里搜索,但是我只找到有关旧api或我目前正在使用的内容或完全不相关的问题的答案。
您可以像下面这样仅resolve
针对根目录创建子目录的名称Path
:
public static void main(String[] args) {
// definition of the root directory (adjust according to your system)
String currentPath = "C:\\";
Path root = Paths.get(currentPath);
// get a specific subdirectory by its name
String specificSubDir = "temp";
// resolve its name against the root directory
Path specificSubDirPath = root.resolve(specificSubDir);
// and check the result
if (Files.isDirectory(specificSubDirPath)) {
System.out.println("Path " + specificSubDirPath.toAbsolutePath().toString()
+ " is a directory");
} else {
System.err.println("Path " + specificSubDirPath.toAbsolutePath().toString()
+ " is not a directory");
}
}
由于我有一个目录C:\temp\
,因此系统上的输出为
Path C:\temp is a directory
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句