昨天老大叫我整一个这样的程序,淦 快下班的时候扔给我, 害 ,没得办法 整一个呗
大概的意思就是读取文件夹中的txt文件中的字段 和视频文件夹中的视频文件名 进行比较 ,如果相同的话。就进行替换
大概就是这么个意思
直接上代码
package com.example.modification;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author
* @description:
* @date :2021/1/28 18:08
*/
public class a {
// 去除文件路径中的不必要符号
public static String replaceNotChinese(String str) {
return str.replaceAll("[0-9a-zA-Z\\.\\-、,?]", "");
}
/**
*
* @param folder 文件路径
* @param type 文件类型
* @return
*/
public static List<String> getAllFileInFold(String folder, String type) {
List<String> files = new ArrayList<String>();
LinkedList<String> folderList = new LinkedList<String>();
folderList.add(folder);
while (folderList.size() > 0) {
File file = new File(folderList.peekLast());
folderList.removeLast();
File[] fs = file.listFiles();
if (null != fs && fs.length > 0) {
ArrayList<File> fileList = new ArrayList<File>();
for (int i = 0; i < fs.length; i++) {
if (fs[i].isDirectory()) {
if (!fs[i].isHidden()) {
folderList.add(fs[i].getPath());
}
} else {
if (!fs[i].isHidden()) {
if (fs[i].getName().lastIndexOf(type) > 1) {
fileList.add(fs[i]);
}
}
}
}
//同一个目录下的文件,排在一起
if (fileList.size() > 0) {
for (File f : fileList) {
files.add(f.getPath());
}
}
}
}
return files;
}
public static boolean renameFile(String oriFile, String nowFile) {
File file = new File(oriFile);
return file.renameTo(new File(nowFile));
}
/**
*
* @param tspath txt 文件路径
* @param mppath MP4 视频路径
*
*/
public static void main(String[] args) {
try {
String tspath = "D:\\tt";
String mppath = "E:\\34、护理";
Map<String, String> tsmap = new HashMap<String, String>();
List<String> mpfiles = getAllFileInFold(mppath, ".mp4");
List<String> tsfiles = getAllFileInFold(tspath, ".txt");
BufferedReader br = null;
for (String file : tsfiles) {
br = new BufferedReader(new FileReader(new File(file)));
String line = null;
while ((line = br.readLine()) != null) {
String[] tss = line.split("\\t");
String key = replaceNotChinese(tss[2].trim()) +
replaceNotChinese(tss[4].trim()) +
replaceNotChinese(tss[6].trim()) +
replaceNotChinese(tss[7].trim());
tsmap.put(key, tss[8].trim());
}
br.close();
}
System.out.println(tsmap.size());
//mp4 files
for (String file : mpfiles) {
String[] mss = file.split("\\\\");
String key = replaceNotChinese(mss[mss.length - 4].trim()) +
replaceNotChinese(mss[mss.length - 3].trim()) +
replaceNotChinese(mss[mss.length - 2].trim()) +
replaceNotChinese(mss[mss.length - 1].trim());
System.out.println(file);
key = key.trim();
System.out.println("key: " + key);
if (tsmap.get(key) != null) {
//rename
String tofile = tsmap.get(key);
tofile = tofile.substring(0, tofile.length() - 3);
tofile = file.substring(0, file.lastIndexOf("\\") + 1) + tofile + ".mp4";
//rename
System.out.println("find: " + tofile);
renameFile(file, tofile);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}