今日份代码:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; 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 { //输出文件目录 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"); System.out.println(String.format("entry.json文件中获取title:%s", thisFileTitle)); //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文件到新目录下并完成重命名动作 File dest = new File(outputPath+thisFileTitle+".m4s"); copyFileUsingFileStreams(fiveLevelFiles[0], dest); System.out.println(String.format("拷贝audio.m4s文件到新目录下并完成重命名动作,全新文件地址:%s", dest.getAbsolutePath())); num++; } System.out.println(String.format("实际生成目标文件数量:%s,是否和预期数量一致:%s", num, num.equals(twoLvelFiles.length))); } private static void 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); } } finally { input.close(); output.close(); } } }
控制台记录过程日志:
一级路径:D:\userOwner\bili\input, 是否存在:true 二级目录总数量:1 二级路径名称:48535790 三级目录总数量是否为1:true,三级路径名称:c_84996945 四级目录总数量是否为3:true,是否存在entry.json文件:true,是否存在16文件夹:true entry.json文件中获取title:【官方MV】萌托Thomas Rhett新单《Look What God Gave Her》首播 五级目录总数量是否为3:true,是否存在audio.m4s文件:true 拷贝audio.m4s文件到新目录下并完成重命名动作,全新文件地址:D:\userOwner\bili\output\【官方MV】萌托Thomas Rhett新单《Look What God Gave Her》首播.m4s 实际生成目标文件数量:1,是否和预期数量一致:true
标签:复制到,String,format,指定,System,File,println,out From: https://www.cnblogs.com/sxdcgaq8080/p/16952091.html