一、读取文件夹下的文件
方法
//读取某个路径下的文件名
public static void methodName(String filePath){
File file = new File(filePath);
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()) {
methodName(file1.getAbsolutePath());
}else {
String name1 = file1.getName();
String s = handleStrWins(name1);
System.out.println(s);
}
}
}
//处理str字段
public static String handleStrWins(String str){
str = str.substring(0, str.indexOf(".")).replace(" ","")
.replace("、","")
.replace("/","");
return str;
}
二、测试方法
public static void main(String[] args){
String filePath1 = "G:\\项目文件\\协议 - 根据内容修改文件名";
methodName(filePath1);
}

本文介绍了一种使用Java读取指定路径下所有文件,并根据文件内容进行重命名的方法。通过递归遍历目录,获取文件名并进行处理,实现了文件的批量重命名功能。
4825

被折叠的 条评论
为什么被折叠?



