1.java实现复制一个文件夹下的所有文件到另一个文件夹并重命名文件
package com.bings.utils;import java.io.File;import java.nio.file.Files;import java.util.ArrayList;import java.util.List;public class ReFileName { public static void main(String[] args) { String basePath = "D:\\upload\\book_img\\old"; File dir = new File(basePath); List<File> allFileList = new ArrayList<>(); // 判断文件夹是否存在 if (!dir.exists()) { System.out.println("目录不存在"); return; } getAllFile(dir, allFileList); for(int i = 0;i<allFileList.size();i++){ //把这些文件重命名 String path = "D:\\upload\\book_img"; File pf=new File(path); if(!pf.exists()){ pf.mkdirs(); } String fileName = i+1+".png"; File targetFile = new File(path+"/"+fileName); try { Files.copy(allFileList.get(i).toPath(), targetFile.toPath());// targetFile.createNewFile(); } catch (Exception e) { e.printStackTrace(); } } } public static void getAllFile(File fileInput, List<File> allFileList) { // 获取文件列表 File[] fileList = fileInput.listFiles(); assert fileList != null; for (File file : fileList) { if (file.isDirectory()) { // 递归处理文件夹 // 如果不想统计子文件夹则可以将下一行注释掉 getAllFile(file, allFileList); } else { // 如果是文件则将其加入到文件数组中 allFileList.add(file); } } }}