【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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2018-12-05 【docker-compose】docker-compose.yml文本内容详解 + docker-compose命令详解 + docker-compose启动服务容器时区设置
2018-12-05 【docker】docker基础原理,核心技术简介
2018-12-05 【docker-compose】使用docker-compose部署运行spring boot+mysql 【处理容器的时区问题】【详解】【福利:使用docker-compose构建 wordpress+mysql】
2018-12-05 【docker-compose】使用docker-compose启动spring-boot+mysql,服务内连数据库报错:create connection SQLException, url: jdbc:mysql://mysql:33061/swapping?useSSL=false&useUnicode=true&characterEncoding=UTF-8, errorCode 0,
2016-12-05 【SQL 触发器】