Java小工具 根据文本批量修改文件名

功能

能够根据使用路径修改文件名,已经测试,能够成功运行工具

思路

先是读取到txt文本文件,以后使用String的spilt进行分割,每一行的格式为 旧名字 新名字,中间的空格能够使用|或者其余字符代替,以此为标志分割String测试

以后将旧名字当作key,新名字当作value写入到map中去编码

得到文件的所在的文件夹,listFile遍历获得全部的文件,以后getName得到文件名(这里得到到的文件名是包括有扩展名的)code

再次使用String的spilt进行处理(参数为"\.",须要转义),获得文件名和扩展名对象

reName更名字,参数为一个文件对象游戏

代码

先把代码贴出来吧,以后再作个有界面的工具~资源

class Test {
    
        private static Map<String, String> map;
    
        public static void main(String[] args) {
            map = readTxtFile("T:\\游戏资源\\仙剑4\\音乐目录.txt");
            reName("T:\\游戏资源\\仙剑4\\仙剑奇侠传四音乐");
    
        }
    
        /**
         *
         * @param s 文件所在文件夹路径名
         */
        public static  void reName(String s){
            File file = new File(s);
            if (file.isDirectory()){
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    String h = files[i].getName();
                    String[] temp = h.split("\\.");
                    String newName = map.get(temp[0]);
                    files[i].renameTo(new File(s+"\\"+newName+"."+temp[temp.length-1]));
                }
            }
        }
        private  static HashMap<String, String> readTxtFile(String filePath){
            HashMap<String, String> map = new HashMap<>();
            try {
                String encoding="GBK";
                File file=new File(filePath);
                if(file.isFile() && file.exists()){ //判断文件是否存在
                    InputStreamReader read = new InputStreamReader(
                            new FileInputStream(file),encoding);//考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while((lineTxt = bufferedReader.readLine()) != null){
                        String[] split = lineTxt.split(" ");
                        map.put(split[0],split[1]);
                    }
                    read.close();
                }else{
                    System.out.println("找不到指定的文件");
                }
            } catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            }
            return map;
        }
    }
相关文章
相关标签/搜索