package com.meng.javase;
import java.io.*;
public class KaoBeiTest02 {
//放到想要放的目錄下,比如我要放到E盤menglalalala目錄下,如果沒有會新建的
static String nf = "E:\\什么是Build Path";
public static void main(String[] args) {
//首先判斷目錄或者文件是否存在,不存在就創建
File newFile = new File(nf);
if (!newFile.exists()) {
newFile.mkdirs();
}
//要提取的文件
File file = new File("E:\\嗶哩嗶哩\\461754827");
//把視頻復制到指定文件夾並根據info文件信息更改文件名
getDirName(file);
deleteOtherFile(file);
System.out.println("文件移動改名成功,並將源文件刪除!");
}
private static void getDirName(File file) {
String newFileName = null;
//遍歷獲得biliFile文件對象,獲得里面每一個文件和文件夾對象
File[] files = file.listFiles();
//再遍歷獲得的對象,進行判斷
for (File fn : files) {
//判斷是否是文件
if (fn.isFile()) {
//是文件,就獲取該文件對象名的字符串
String fileName = fn.getName();
//1.判斷文件名是否是以info結尾
if (fileName.endsWith("info")) {
try {
//新建字符輸入流
BufferedReader br = new BufferedReader(new FileReader(fn));
//讀取文件中第一整行的字符串
String line = br.readLine();
//把讀取到的字符串,先按照字符串"PartName":"進行切割成兩個部分,並存入split1數組中
String[] split1 = line.split("\"PartName\":\"");
//再對后半部的字符串按照字符串","進行切割並存入name數組中
String[] split2 = split1[1].split("\",\"");
//找"PartNo":"
String[] split3=line.split("\"PartNo\":\"");
//再找","后面的視頻順序號
String[] split4=split3[1].split("\",\"");
//那么split2數組中0索引處的字符串就是我們需要提取的文件
//拼接新文件名路徑字符串
newFileName = split4[0]+"_"+split2[0] + ".mp4";
br.close();
} catch (Exception e) {
e.printStackTrace();
}
//2.判斷文件名是否是以mp4結尾
} else if (fileName.endsWith("mp4")) {
String oldPath =fn.getAbsolutePath();
String newPath =nf+"\\"+newFileName;
File nfp=new File(newPath);
boolean b= fn.renameTo(nfp);
if(b==false){
System.out.println("文件"+newFileName+"移動失敗");
System.exit(0);
}
}
} else {
//是文件夾就繼續遞歸
getDirName(fn);
}
}
}
public static void deleteOtherFile(File file){
File[] files=file.listFiles();
for(File file1:files){
if(file1.isFile()){
file1.delete();
System.out.println("成功delete");
}else{
deleteOtherFile(file1);
}
}
file.delete();
}
}
引用下面博主的
https://blog.csdn.net/WECHS2017/article/details/115249603?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-1.control&spm=1001.2101.3001.4242