/**
* @Description: TODO
* @Author: yangyb
* @Date:2022/7/18 23:11
* Version: 1.0
**/
public class InterceptMore {
public static void main(String[] args) {
String filePath="D:\\test\\test.bat";
//按‘\’截取
String[] split = filePath.split("\\\\");
//最后一个就是文件名
String fileName = split[split.length - 1];
//将文件名从文件路径中用空字符串给替掉,就相当于去掉了
String newFilePath = filePath.replace(fileName, "");
System.out.println("截取的文件名:"+fileName);
System.out.println("截取的文件路径:"+newFilePath);
}
}
运行结果:
/**
* @Description: 截取文件路径和文件名
* @Author: yangyb
* @Date:2022/7/11 23:37
* Version: 1.0
**/
public class Intercept_ {
public static void main(String[] args) {
String filePath="D:\\test\\test.bat";
//按‘\’截取
String[] split = filePath.split("\\\\");
//最后一个就是文件名
String fileName = split[split.length - 1];
System.out.println("截取的文件名:"+fileName);
StringBuilder stringBuilder = new StringBuilder();
//将前面截取的路径再拼接起来即可
for (int i=0;i<split.length-1;i++){
stringBuilder.append(split[i]);
stringBuilder.append("\\");
}
//文件路径名
String newFilePath=stringBuilder.toString();
System.out.println("截取的文件路径:"+newFilePath);
}
}