Java中截取路径的代码
假设要截取的路径为path:
- 获取文件名(包括扩展名):
String fileName = new File(path).getName();
- 获取文件父目录路径:
String parentPath = new File(path).getParent();
- 获取文件扩展名(不包括点号):
String extension = "";
int index = path.lastIndexOf('.');
if (index > 0 && index < path.length() - 1) {
extension = path.substring(index + 1);
}
- 获取文件名(不包括扩展名):
String nameWithoutExtension = "";
int index = path.lastIndexOf('.');
if (index > 0 && index < path.length() - 1) {
nameWithoutExtension = path.substring(0, index);
}
原文地址: https://www.cveoy.top/t/topic/blNu 著作权归作者所有。请勿转载和采集!