更改歌曲后缀-代码备忘录

发布时间 2023-05-01 20:40:15作者: 三号小玩家
package com.java1234.todo;

import java.io.*;
import java.util.HashMap;
import java.util.Objects;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  //沈亦晨 - 爱人错过 (小提琴版).flac //留下
 *
 *         //沈亦晨 - 爱人错过 (小提琴版).mp3
 *         //沈亦晨 - 爱人错过 (小提琴版)[mqms].mp3
 *         //沈亦晨 - 爱人错过 (小提琴版)[mqms2].mp3
 *         //沈亦晨 - 爱人错过 (小提琴版).ogg
 *         //沈亦晨 - 爱人错过 (小提琴版)[mqms].ogg
 *         //沈亦晨 - 爱人错过 (小提琴版) [mqms2].ogg
 */
public class FileTodo2 {
    public static void main(String[] args) throws FileNotFoundException {

            String targetDirectory="/root/音乐";
            //搞成文件格式
        File file = new File(targetDirectory);
        //拿到所有文件名
        String[] list = file.list();
        HashMap<String, String> flacMap = new HashMap<>();

        for (int i = 0; i < list.length; i++) {
          String str=  list[i];
            //System.out.println(str);
            /// ---- -   --  -

            if(str.contains("flac")){
                flacMap.put(str.substring(0,str.indexOf(".flac")),"flac");
            }

        }

        for (int i = 0; i < list.length; i++) {
            String str1=  list[i];
            if(str1.contains("mp3")){
                //如果有,则删除
                String newstr1= "";

                String newstr2= "";
                try {
                    newstr2 = str1.substring(0,str1.indexOf("[mqms2].mp3"));
                } catch (Exception e) {
                    newstr2="";
                }
                String newstr3= "";
                try {
                    newstr3 = str1.substring(0,str1.indexOf("[mqms].mp3"));
                } catch (Exception e) {
                    newstr3="";
                }
                try {
                    if("".equals(newstr2)&&"".equals(newstr3)){
                        newstr1 = str1.substring(0,str1.indexOf(".mp3"));
                    }
                } catch (Exception e) {
                    newstr1="";
                }
                if(flacMap.containsKey(newstr1)||flacMap.containsKey(newstr2)||flacMap.containsKey(newstr3)){
                  //判断哪种,直接删除
                  File delfile = new File(targetDirectory +File.separator+ str1);
                  boolean delete = delfile.delete();
                  System.out.println(delfile);
              }else{
                    String newkey=newstr1+newstr2+newstr3;
                  //留下来
                    File updateNameFile = new File(targetDirectory+File.separator+ str1);
                    File renamefile = new File(targetDirectory + File.separator +newkey+ ".flac");
                    boolean flag = updateNameFile.renameTo(renamefile);
                     flacMap.put(newkey,"flac");
                  //转完之后加入到flac中
              }
             }
        }


       

        for (int i = 0; i < list.length; i++) {
            String oggstr1=  list[i];
            if(oggstr1.contains("ogg")){
                //如果有,则删除
                String ogg_newstr1= "";

                String newstr2="";
                try {
                    newstr2 = oggstr1.substring(0,oggstr1.indexOf("[mqms2].ogg"));
                } catch (Exception e) {
                    newstr2="";
                }
                String newstr3= "";
                try {
                    newstr3 = oggstr1.substring(0,oggstr1.indexOf("[mqms].ogg"));
                } catch (Exception e) {
                    newstr3="";
                }
                try {

                    if("".equals(newstr2)&&"".equals(newstr3)){
                        ogg_newstr1 = oggstr1.substring(0,oggstr1.indexOf(".ogg"));

                    }
                } catch (Exception e) {
                    ogg_newstr1="";
                }

                if(flacMap.containsKey(ogg_newstr1)||flacMap.containsKey(newstr2)||flacMap.containsKey(newstr3)){
                    //判断哪种,直接删除
                    File delfile = new File(targetDirectory +File.separator+ oggstr1);
                    boolean delete = delfile.delete();
                    System.out.println(delfile);
                }else{
                    String newkey=ogg_newstr1+newstr2+newstr3;
                    //留下来
                    File updateNameFile = new File(targetDirectory+File.separator+ oggstr1);
                    File renamefile = new File(targetDirectory + File.separator +newkey + ".flac");
                    boolean flag = updateNameFile.renameTo(renamefile);
                     flacMap.put(newkey,"flac");
                }
            }
        }


    }



}