【java】【File】用File相关类写一个小工具,完成从指定目录下抽取指定文件并复制到新路径下完成重命名的功能

今日份代码:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class FileTest {

    public static void main(String[] args) throws IOException, InterruptedException {

        //输出文件目录
        String outputPath = "D:\\userOwner\\bili\\output\\";

        //1.指定一级路径
        String path = "D:\\userOwner\\bili\\input";
        File oneLevelFile = new File(path);
        System.out.println(String.format("一级路径:%s, 是否存在:%s", oneLevelFile.getAbsolutePath(),oneLevelFile.exists()));

        //2.获取二级路径
        File[] twoLvelFiles = oneLevelFile.listFiles();
        System.out.println(String.format("二级目录总数量:%s", twoLvelFiles.length));

        Integer num = 0;
        for (File twoLvelFile : twoLvelFiles) {
            System.out.println(String.format("二级路径名称:%s", twoLvelFile.getName()));

            //3.进入三级路径
            File[] threeLevelFiles = twoLvelFile.listFiles();
            System.out.println(String.format("三级目录总数量是否为1:%s,三级路径名称:%s",
                    threeLevelFiles.length == 1, threeLevelFiles[0].getName()));
            File threeLevelFile = threeLevelFiles[0];

            //4.进入四级路径
            File[] fourLevelFiles = threeLevelFile.listFiles();
            System.out.println(String.format("四级目录总数量是否为3:%s,是否存在entry.json文件:%s,是否存在16文件夹:%s",
                    fourLevelFiles.length == 3, fourLevelFiles[2].getName().equals("entry.json"),  fourLevelFiles[0].getName().equals("16")));


            //5.从entry.json文件获取对应title
            String thisFileTitle = "";
            File entryJsonFile = fourLevelFiles[2];
            List<String> entryJsonStrList = Files.readAllLines(Paths.get(entryJsonFile.getAbsolutePath()), StandardCharsets.UTF_8);
            JSONObject jsonObject = JSON.parseObject(entryJsonStrList.get(0));
            thisFileTitle = jsonObject.getString("title");
            String[] split1 = thisFileTitle.split("》");
            String newFileTitle = split1[0];
            String[] split2 = newFileTitle.split("《");
            newFileTitle = split2.length >1 ? split2[1] : split2[0];
            newFileTitle = newFileTitle.replaceAll(" / ", "");
            newFileTitle = newFileTitle.replaceAll("\\?", "");
//            newFileTitle = newFileTitle.replaceAll(" | ", "");
            newFileTitle = newFileTitle.replaceAll(" & ", "");
            newFileTitle = newFileTitle.replaceAll("\\**", "");
            newFileTitle = newFileTitle.replaceAll(" - ", "");
            newFileTitle = newFileTitle.replaceAll(",", "-");
            newFileTitle = newFileTitle.replaceAll("|", "");
            System.out.println(String.format("entry.json文件中获取title:%s,newTitle=%s", thisFileTitle, newFileTitle));


            //6.从16文件夹中进入五级路径
            File[] fiveLevelFiles = fourLevelFiles[0].listFiles();
            System.out.println(String.format("五级目录总数量是否为3:%s,是否存在audio.m4s文件:%s",
                    fiveLevelFiles.length == 3, fiveLevelFiles[0].getName().equals("audio.m4s")));


            //7.拷贝audio.m4s文件到新目录下并完成重命名动作
            try {
                File dest = new File(outputPath+newFileTitle+".m4s");
                if (dest.exists()) {
                    dest = new File(outputPath+"2"+dest.getName());
                    System.out.println(String.format("目标文件已存在:%s,重命名2全新文件地址:%s",
                            outputPath+newFileTitle+".m4s",dest.getName() ));
                }
                String resultPath = copyFileUsingFileStreams(fiveLevelFiles[0], dest);
                System.out.println(String.format("拷贝audio.m4s文件到新目录下并完成重命名动作,全新文件地址:%s",
                        resultPath));
                if (dest.exists() && StringUtils.isNotBlank(resultPath)) {
                    num++;
                }else {
                    System.out.println(String.format("拷贝audio.m4s文件到新目录下并完成重命名动作,文件不存在,二级目录:%s",
                            twoLvelFile.getName()));
                }
            }catch (Exception e) {
                System.out.println(String.format("拷贝audio.m4s文件到新目录下并完成重命名动作,发生异常, 二级目录:%s",
                        twoLvelFile.getName()));
            }


        }


        System.out.println(String.format("实际生成目标文件数量:%s,是否和预期数量一致:%s",
                num, num.equals(twoLvelFiles.length)));
    }


    private static String copyFileUsingFileStreams(File source, File dest)
            throws IOException {

        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
            System.out.println("执行正常:"+ dest.getAbsolutePath());
        }finally {
            input.close();
            output.close();
        }
        return dest.getAbsolutePath();
    }
}

 

 

 

 

控制台记录过程日志:

一级路径:D:\userOwner\bili\input, 是否存在:true
二级目录总数量:390
二级路径名称:205150021
三级目录总数量是否为1:true,三级路径名称:c_328651402
四级目录总数量是否为3:true,是否存在entry.json文件:true,是否存在16文件夹:true
entry.json文件中获取title:曾火遍全球的一首《Going Home》,旋律空灵唯美,王菲都曾翻唱过,newTitle=GoingHome
五级目录总数量是否为3:true,是否存在audio.m4s文件:true
执行正常:D:\userOwner\bili\output\GoingHome.m4s
拷贝audio.m4s文件到新目录下并完成重命名动作,全新文件地址:D:\userOwner\bili\output\GoingHome.m4s
.....
.....
实际生成目标文件数量:386,是否和预期数量一致:false

 

posted @ 2022-12-05 13:49  Angel挤一挤  阅读(173)  评论(0编辑  收藏  举报