JAVA实现批量修改文件名称几种方法

JAVA实现批量修改文件名称几种方法

其实就是代码实现如下:

package filenamecopy;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class renameuserfilename {
	
	
	
	/****
	 *   String类可能使用的方法
		
		substring(int beginIndex) 
		         截取从beginIndex到末尾的字符串并返回
		substring(int beginIndex, int endIndex) 
		         截取从beginIndex到endIndex的字符串并返回 
		concat(String str) 
		          将指定字符串str连接到此字符串的结尾
		indexOf(int ch) 
		          返回指定字符在此字符串中第一次出现处的索引
		indexOf(String str) 
		          返回指定子字符串在此字符串中第一次出现处的索引
		lastIndexOf(int ch) 
		          返回指定字符在此字符串中最后一次出现处的索引
		lastIndexOf(int ch, int fromIndex) 
		          返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索 
		length()   返回此字符串的长度
		replace(CharSequence target, CharSequence replacement) 
		          使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串
		replaceAll(String regex, String replacement) 
		          使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串

	 * @param args
	 * @throws IOException
	 */

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		// 在目标文件夹下重命名
		 renamfile1("D:\\解密\\test\\10\\spark");
        //拷贝别的目录下重命名
		//reNameFile3("D:\\解密\\test\\10\\spark", "E:\\test\\10");

	}

	public static void reNameFile3(String oldfiles, String newfile)
			throws IOException {

		// File folder = new File("D:\\解密\\test\\10\\spark");
		File folder = new File(oldfiles);
		File newfiles = new File(newfile);
		// 如果不存在,创建文件夹
		if (!newfiles.exists()&&!newfiles.isDirectory()) {

			/*
			 * 创建单级目录
			 * System.out.println("=====创建文件夹==========="+newfiles.mkdir());
			 * newfiles.mkdir();
			 */
			// 创建多级目录
			newfiles.mkdirs();
			System.out.println("=====创建文件夹===========" + newfiles.mkdirs());
		
		}
		// 获取该目录下所有文件的File数组
		File[] fileArray = folder.listFiles();
		int i = 1;
		for (File f : fileArray) {

			// System.out.println(f);
			String name = f.getName();
			int index = name.indexOf(":");
			String newName = i + "." + name.substring(index + 1, name.length());
			System.out.println(newName);
			// File newFile = new File(folder,newName);
			// f.renameTo(newFile);
			i++;
			// 封装数据源
			/*
			 * //FileReader fr = new FileReader("c:\\a.txt"); FileReader fr =
			 * new FileReader(oldfiles+"\\"+name); // 封装目的地 //FileWriter fw =
			 * new FileWriter("d:\\b.txt"); FileWriter fw = new
			 * FileWriter(newfile+"\\"+newName); // 读写数据 // int ch = 0; int ch;
			 * while ((ch = fr.read()) != -1) { fw.write(ch); }
			 * 
			 * //释放资源 fw.close(); fr.close();
			 */

			// 封装数据源
			BufferedReader br = new BufferedReader(new FileReader(oldfiles
					+ "\\" + name));
			// 封装目的地
			BufferedWriter bw = new BufferedWriter(new FileWriter(newfile
					+ "\\" + newName));

			// 读写数据
			String line = null;

			// 两种方式其中的一种一次读写一个字符数组
			char[] chs = new char[1024];
			int len = 0;
			while ((len = br.read(chs)) != -1) {
				bw.write(chs, 0, len);
				bw.flush();
			}

			// 释放资源
			bw.close();
			br.close();

		}

	}

	public static void reNamefile2(String oldfiles) {

		// File folder = new File("D:\\解密\\test\\10\\spark");
		File folder = new File(oldfiles);
		// 获取该目录下所有文件的File数组
		File[] fileArray = folder.listFiles();

		if (!folder.exists()) {
			return;// 重命名文件不存在
		}

		int i = 1;
		for (File f : fileArray) {

			// System.out.println(f);
			String name = f.getName();
			int index = name.indexOf(":");
			String newName = i + "." + name.substring(index + 1, name.length());
			System.out.println(newName);
			File newFile = new File(folder, newName);
			f.renameTo(newFile);
			i++;

		}

	}

	public static void renamfile1(String oldfiles) throws IOException {
		
		File file = new File(oldfiles);

		File[] list = file.listFiles();
		// 如果目录下文件存在
		if (file.exists() && file.isDirectory()) {
			for (int i = 0; i < list.length; i++) {
				// 取文件名子存入name中
				String name = list[i].getName();
				// 截取":"之前的字符串出来
				int index = name.indexOf(":");
				String name2 = name.substring(index + 1);
				// 重命名并存入
				int j = i + 1;
				
				File dest = new File(oldfiles + "\\" + j + "." + name2);
				list[i].renameTo(dest);
				  System.out.println(dest.getName());

				/*
				 * if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同,则不允许重命名
				 * System.out.println(newname+"已经存在!"); else{
				 * oldfile.renameTo(newfile); }
				 */

			}
		}
	}

	public static void reNameFile0(String filespath){
		  File file = new File(filespath);
	        File[] list = file.listFiles();

	        // 如果目录下文件存在
	        if (file.exists() && file.isDirectory())
	        {
	            for (int i = 0; i < list.length; i++)
	            {
	                //取文件名子存入name中
	                String name = list[i].getName();
	                // 截取.之前的字符串出来
	                int index = name.indexOf(".");
	                // 截取后缀名字.qsv/.exe/.avi/.mp4出来
	                int index2 = name.lastIndexOf(".");
	                String name3 = name.substring(index2);
	                // 拼接字符串
	                String newName = "2019-4-18-" + (i + 1) + name3;
	                //重命名
	                File dest = new File(filespath + "\\" + newName);
	                list[i].renameTo(dest);
	                System.out.println(dest.getName());
	            }
	        }

	}
	
	
	
	
}

处理前:

处理后:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值