扫描单文件夹
File fileTest = new File("D:\\测试文件夹");
String[] fileList = fileTest.list();
for (int i = 0; i < fileList.length; i++) {
String string = fileList[i];
File file = new File(fileTest.getPath(), string);
int i1 = string.lastIndexOf(" ");
if(i1 == -1){
continue;
}
String s = string.substring(i1);
File fileNew = new File(fileTest.getPath(), s);
file.renameTo(fileNew);
}
递归扫描所有文件和文件夹
public void find(String pathName) throws IOException {
//获取pathName的File对象
File dirFile = new File(pathName);
//获取此目录下的所有文件名与目录名
String[] fileList = dirFile.list();
for (int i = 0; i < fileList.length; i++) {
//遍历文件目录
String string = fileList[i];
File file = new File(dirFile.getPath(), string);
int i1 = string.lastIndexOf(" ");
if(i1 == -1){
continue;
}
String s = string.substring(i1);
File fileNew = new File(dirFile.getPath(), s);
file.renameTo(fileNew);
//如果是一个目录,输出目录名后,进行递归
if (fileNew.isDirectory()) {
//递归
find(fileNew.getCanonicalPath());
}
}
}
find("D:\\测试文件夹");