栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

电视剧和电影和照片的批量重命名的代码,注意:并不包括全部都能使用,只能使用有规律的

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

电视剧和电影和照片的批量重命名的代码,注意:并不包括全部都能使用,只能使用有规律的

package FileRenamed;

import java.io.File;
import java.util.Scanner;


public class FileRenamed {
	public static void main(String[] args) {
		while (true) {
			Scanner sc = new Scanner(System.in);
			System.out.print("请输入根目录:");
			String path = sc.next();
			if (path.contains("电视剧")) {
				ranamedOne(path);
			} else if (path.contains("照片")) {
				ranamedTwo(path);
			} else if (path.contains("电影")) {
				ranamedThree(path);
			}
		}
	}

	
	public static void ranamedOne(String path) {
		File file = new File(path);
		File[] fileList = file.listFiles();
		int i = 0, number = 0;
		for (File files : fileList) {
			// 获取文件的后缀名(.xxx)
			String ext = files.getName().substring(files.getName().lastIndexOf("."));
			// 获取文件中的数字,就是哪一集的数字,进行替换
			if (files.getName().contains("第") && files.getName().contains("集")) {
				number = Integer.parseInt(
						files.getName().substring(files.getName().lastIndexOf("第") + 1, files.getName().indexOf("集")));
			} // 文件中的名字
			String sname = file.getName();
			// 组合新的文件名
			StringBuffer sbf = new StringBuffer();
			if (number != 0) {
				sbf.append(path + "/" + sname + " 第" + number + "集" + ext);
			} else {
				sbf.append(path + "/" + sname + ext);
			}
			// 进行重命名
			boolean b = files.renameTo(new File(sbf.toString()));
			if (b == true) {
				i++;
			}
			if (i == fileList.length) {
				System.out.println("该文件夹下面的文件已经全部重命名完成");
			}
		}
	}

	
	public static void ranamedTwo(String path) {
		File dir = new File(path);
		File[] files = dir.listFiles();
		int x = 0;
		for (int i = 0; i < files.length; i++) {
			String ext = files[i].getName().substring(files[i].getName().lastIndexOf("."));
			StringBuffer sbf = new StringBuffer(path + "/" + (i + 1) + ext);
			boolean b = files[i].renameTo(new File(sbf.toString()));
			if (b == true) {
				x++;
			}
			if (x == files.length) {
				System.out.println("该文件夹下面的文件已经全部重命名完成");
			}
		}
	}

	
	public static void ranamedThree(String path) {
		File file = new File(path);
		File[] files = file.listFiles();
		int x = 0;
		for (int i = 0; i < files.length; i++) {
			String sname = files[i].getName().substring(0, files[i].getName().indexOf("."));
			if (files[i].getName().contains("-")) {
				sname = files[i].getName().substring(0, files[i].getName().indexOf("-"));
			}
			String ext = files[i].getName().substring(files[i].getName().lastIndexOf("."));
			StringBuffer sbf = new StringBuffer(path + "/" + sname + ext);
			boolean b = files[i].renameTo(new File(sbf.toString()));
			if (b == true) {
				x++;
			}
			if (x == files.length) {
				System.out.println("该文件夹下面的文件已经全部重命名完成");
			}
		}
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/284230.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号