给定这样的文件路径:
Path filePath; // "/Users/me/Desktop/archive.zip"
我想要一个函数,通过删除其文件扩展名将此文件路径转换为目录路径,并将文件路径视为目录路径。例如:
Path dirPath = asDirectoryPath(filePath); // "/Users/me/Desktop/archive"
我尝试了什么?
好吧,我有一个解决方案,我认为这很丑陋:
private Path asDirectoryPath(Path filePath)
{
return Paths.get(path.toString().substring(0, path.toString().lastIndexOf('.')));
}
为什么丑陋?因为我被迫执行路径进行字符串转换并搜索“。”。符号,假设以下字符是文件扩展名。此功能应该是可移植的,并且应该在Windows和UNIX系统中均可以使用。还有其他正确的解决方案吗?谢谢。
幕布斯6054654
茅侃侃
相关分类