OnePiece改名

今天写了个程序把海贼王的目录下的文件名整理了下

 

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);
			}
		}
	}
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值