版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/new_Lcc/article/details/79434009
使用JAVA代码根据JSON文件进行批量修改文件名以及路径
某些网站视频加密分割,首先去下载格式转换工具,再用chrome F12打开控制台,找到json文件进行文件还原
json数据:
{“message”:“hello”,“result”:[],“status”:200}
步骤:
1、先根据你的json文件在线生成JavaBean.
2、上代码:
代码块
package com.gogo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.besjon.pojo.*;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by gogo
*/
@SuppressWarnings("all")
public class ConvertorFile {
public static void main(String[] args) throws IOException {
File file = new File(Thread.currentThread().getContextClassLoader().getResource("json.txt").getFile());
String json = FileUtils.readFileToString(file);
JsonRootBean jsonRootBean = JSON.parseObject(json, new TypeReference<JsonRootBean>() {
});
List<Result> result = jsonRootBean.getResult();
Date date1 = new Date();
String path = "F:\\temp\\cache"; // 要批量修改的文件所在的目录
File filePath = new File(path);
File ff = null;
String newFileName = ""; // 新的文件名字
String oldFileName = ""; // 旧的文件名字
String path01 = "";
String path02 = "";
String newPath = "";
for (File f : filePath.listFiles()) {
oldFileName = f.getName().substring(0, f.getName().indexOf("."));
List<ChapterSons01> chapterSons01 = new ArrayList<>();
List<ChapterSons02> chapterSons02 = new ArrayList<>();
List<Mp3s> mp3s = new ArrayList<>();
for (Result result_ : result) {
chapterSons01 = result_.getChapterSons01();
for (ChapterSons01 sons01 : chapterSons01) {
chapterSons02 = sons01.getChapterSons02();
for (ChapterSons02 sons02 : chapterSons02) {
mp3s = sons02.getMp3s();
for (Mp3s mp3 : mp3s) {
if (oldFileName.equals(mp3.getId())) {
newFileName = mp3.getMp3Name() + ".mp3";
String chapterId = mp3.getChapterId();
path02 = sons01.getName();
path01 = result_.getName();
newPath = "F:\\temp\\Test\\" + path01 + "\\" + path02;
ff = new File(newPath);
if (!ff.exists() || !ff.isDirectory()) {
ff.mkdirs();
}
File newFile = new File(newPath, newFileName);
System.out.println(f.renameTo(newFile));
}
}
}
}
}
System.out.println(oldFileName + "--->>>" + newFileName);
}
System.out.println("全部完成!");
Date date2 = new Date();
System.out.println(date2.getTime() - date1.getTime());
}
}
注意:
1、使用f.renameTo(file)需要注意file不存在,但是file的最后一个“\”之前的路径必须存在;
2、