package sample;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static List<File> getFiles(String path) {
List<File> files = new ArrayList<>();
File file = new File(path);
File[] tempList = file.listFiles();
if (tempList == null) return files;
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
files.add(tempList[i]);
} else if (tempList[i].isDirectory()) {
//这里就不递归了,
//files.addAll(getFiles(tempList[i].getPath()));
}
}
return files;
}
public static void main(String[] args) {
//这里是路径
String dir = System.getProperty("user.dir");
List<File> files = getFiles(dir);
int index = 1;
for (File file : files) {
String fileName = file.getName();
int lastIndexOf = fileName.lastIndexOf(".");
String suffix = fileName.substring(lastIndexOf);
String path = file.getPath().replace(fileName, "");
File newFile = new File(path + "desk_" + index + suffix);
file.renameTo(newFile);
index++;
}
}
}
网友评论