OnePiece改名
3139 ワード
今日はプログラムを书いて海贼王のディレクトリの下のファイル名を整理しました
ファイルごとにトピック名を追加
public class OnePiece {
public static void main(String[] args) {
String path = "E:\\video\\onepiece";
File file = new File(path);
File[] files = file.listFiles();
String[] names = new String[files.length];
for (int i = 0; i < files.length; i++) {
names[i] = files[i].getName();
}
for (int i = 0; i < names.length; i++) {
String newFileName = AnaString(names[i]);
File newFile = new File(path + "\\" + newFileName);
while(newFile.exists()){
int index=new Random().nextInt(10);
newFile=new File(path+"\\"+newFileName+index);
}
files[i].renameTo(newFile);
}
}
public static String AnaString(String name) {
int index=name.lastIndexOf(".");
if(index==-1)
return name;
String prefix=name.substring(0, index);
String subfix=name.substring(index,name.length());
Pattern pattern = Pattern
.compile("((\\[[0-9]{3}\\])|(\\[([0-9]{3})([-_]*)([0-9]{3}))\\])");
Matcher mather = pattern.matcher(prefix);
boolean isMatch = false;
int count=0;
String newname="";
while (mather.find()) {
isMatch = true;
newname=mather.group();
count++;
}
if(count!=1){
System.out.println("warning");
return name;
}
return newname.replace("[","").replace("]","")+subfix;
}
}
ファイルごとにトピック名を追加
public class AddName {
public static void main(String[] args) throws IOException {
File f = new File("D:\\onepiece.txt");
BufferedReader bw = new BufferedReader(new FileReader(f));
String line = null;
Map<String, String> map = new HashMap<String, String>();
while ((line = bw.readLine()) != null) {
String[] array = StringUtils.split(line, " ", 2);
String key = array[0];
int len = key.length();
while (len < 3) {
key = "0" + key;
len++;
}
map.put(key, array[1]);
}
String path = "E:\\video\\onepiece";
File file = new File(path);
File[] files = file.listFiles();
String[] names = new String[files.length];
for (int i = 0; i < files.length; i++) {
names[i] = files[i].getName();
int index = names[i].lastIndexOf(".");
String prefix = names[i].substring(0, index);
String after = names[i].substring(index, names[i].length());
if (map.containsKey(prefix)) {
String value = map.get(prefix);
String fileName = prefix + " " + value.trim() + after;
File newFile = new File(path + "\\" + fileName);
System.out.println(newFile.getName());
files[i].renameTo(newFile);
}
}
}
}