本地文件批量重命名


public static void updFileName(){
        String rootPath="E:\\work\\XP\\部库\\问题相片";
        File file = new File(rootPath);
        File[] files = file.listFiles();
        for(File fs:files){
            String name=fs.getName();
            File oldName = new File(rootPath+name);
            System.out.println(oldName);//夏小正_230204XXXX0718212X_108.jpg
            String s[]=name.split("_");
            File newName = new File(rootPath+s[1]+".jpg");
            System.out.println(newName);//230204XXXX0718212X.jpg

            if (newName.exists()) {  //确保新的文件名不存在
                try {
                    throw new java.io.IOException("file exists");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //重命名方法
            if(oldName.renameTo(newName)) {
                System.out.println("已重命名");
            } else {
                System.out.println("Error");
            }
        }
    }