按260个文件一个文件夹
public static void main(String[] args) {String path = "F:\\壁纸爬虫合集\\爬虫文件夹No1";int fileCount = 0;List<List<File>> folderList = new ArrayList<>();List<File> folderFiles = new ArrayList<>();for (File file : new File(path).listFiles()) {if (file.isFile()) {folderFiles.add(file);fileCount++;if (fileCount % 260 == 0 || fileCount == new File(path).listFiles().length) {folderList.add(folderFiles);folderFiles = new ArrayList<>();}}}for (int i = 0; i < folderList.size(); i++) {List<File> folder = folderList.get(i);File newFolder = new File(path, "folder_" + i);newFolder.mkdir();for (File file : folder) {file.renameTo(new File(newFolder, file.getName()));}}}
文件批量重命名
public static void main(String[] args) {// 定义原始文件夹路径String folderPath = "F:\\壁纸爬虫合集\\爬虫文件夹No2";// 获取文件夹对象File folder = new File(folderPath);// 获取文件夹中的所有文件File[] files = folder.listFiles();Long count = 20000L;// 遍历文件列表并修改文件名for (File file : files) {if (file.isFile()) {String newFileName = "贰十六_" + String.format("%08d", count++) + file.getName().substring(file.getName().lastIndexOf("."));// 重命名文件if (file.renameTo(new File(folder, newFileName))) {System.out.println("文件名修改成功: " + file.getName() + " -> " + newFileName);} else {System.out.println("文件名修改失败: " + file.getName());}}}}
删除文件小于1.6M的数据
public static void main(String[] args) {// 定义原始文件夹路径String folderPath = "F:\\壁纸爬虫合集\\爬虫文件夹No2";// 获取文件夹对象File folder = new File(folderPath);// 获取文件夹中的所有文件File[] files = folder.listFiles();Long count = 1L;// 遍历文件列表并修改文件名for (File file : files) {if (file.isFile()) {if(file.length() < 1600000){System.out.println(file.getName() + " 文件大小为:" +file.length() + " 小于1.6M 执行删除文件方法");file.delete();}}}}
将文件夹下所有文件复制到新文件夹中
public static void main(String[] args) throws Exception{String path = "F:\\原文件夹";for (File file : new File(path).listFiles()) {if(file.isFile()){Files.copy(file,new File("F:\\新文件夹", (path + "-" + file.getName()).replace(" ","")));}if(file.isDirectory()){for (File f : file.listFiles()) {if(f.isFile()){Files.copy(f,new File("F:\\新文件夹", (file.getName() + "-" + f.getName()).replace(" ","")));}if(f.isDirectory()){for (File s : file.listFiles()) {if(s.isFile()){Files.copy(s,new File("F:\\新文件夹", (f.getName() + "-" + s.getName()).replace(" ","")));}if(s.isDirectory()){for (File x : file.listFiles()) {if(x.isFile()){Files.copy(x,new File("F:\\新文件夹", (s.getName() + "-" + x.getName()).replace(" ","")));}}}}}}}}}