批量修改文件后缀

舍友一把5V5后,我成功开发了可视化文件批量修改后缀的野生脚本!!!

舍友每次从Edge保存的好看的壁纸都是.jfif格式的,发给别人后查看还得改格式,于是动手写了个文件后缀可以批量查看以及修改的野生脚本

1.输入不限文件或文件夹

2.可查阅,目录结构可视化

3.路径保留

效果

直接上图 

 

 

 APICode:

package _tools;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/****
 *
 *@Author:C_Block
 *Function:文件后缀批量修改器
 *
 ****/
public class FileReName{
    private boolean flag=false;
    public FileReName() {
    }
    protected void init(){
        System.out.println("运行过程中,所陈列的File:与_字符之间的空格表示子目录的缩进!");
        Scanner sc=new Scanner(System.in);
        System.out.println("Step1:请输入父目录路径名,例如d:\\java");
        File file=new File(sc.nextLine());
        System.out.println("Step2:请输入欲更改的后缀,例如jpg,没有则输入空格");
        String reg=".*\\."+sc.nextLine()+"$";
        if(!showDirStructure(file,reg)){
            System.out.println("null无此后缀文件!");
            return;
        }
        System.out.println("Step3:请输入新的后缀,例如bmp");
        Rename(file,sc.nextLine());
        sc.close();
    }
    List<String> list= new ArrayList<>();
    String dir="+";
    String wj="_";
    String space="";
    private boolean showDirStructure(File file,String reg){
        if(file.isDirectory()){
            File[] files=file.listFiles();
            System.out.println(space+"-------在"+dir+file.getName()+"目录下找得如下文件------");
            space+="   ";
            assert files != null;
            Arrays.sort(files);
            for (File f:files){
                showDirStructure(f,reg);
            }
            space="";
        }
        else{
            String catch_= file.getName();
            if(catch_.matches(reg)||(!(catch_.matches(".*\\.[a-z]{0,5}$"))&&(catch_+". ").matches(reg))){
                flag=true;
                System.out.println("File:"+space+wj+file.getName());
                if((catch_+". ").matches(reg)){
                    catch_+=".jpg";
                    File newFile=new File(file.getParent(),catch_);
                    file.renameTo(newFile);
                    System.out.println("文件无后缀,已经默认修i改为.jpg格式,若欲修改请继续输入!");
                }
                list.add(catch_);
            }
        }
        return flag;
    }
    private void Rename(File file,String newEnding){
        if(file.isDirectory()){
            File[] files=file.listFiles();
            assert files != null;
            for (File f:files){
                Rename(f,newEnding);
            }
        }
        for(String name:list){
            if(file.getName().equals(name)){
                String newName = name.substring(0,name.lastIndexOf("."))+'.'+newEnding;
                File newFile = new File(file.getParent(),newName);
                System.out.println(newFile);
                file.renameTo(newFile);
            }
        }
    }
}

 TestCode:

package _tools;

public class TestTool {
    public static void main(String[] args) {
    new FileReName().init();
    }
}

 

  代码也可以去gitee上下载:https://gitee.com/elegant-and-vulgar-doctor/file-re-name.githttps://gitee.com/elegant-and-vulgar-doctor/file-re-name.git

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值