JAVA 重命名文件、复制文件夹、拷贝文件的实现

JAVA File实现重命名文件、复制文件夹、拷贝文件

public static void main(String[] args) {
	// 要读取的文件夹路径
	String filepath = "C:\\Users\\admin\\Desktop\\logo";
	File file = new File(filepath);
	if (!file.isDirectory()) {
		System.out.println("文件");
		System.out.println("path=" + file.getPath());
		System.out.println("absolutepath=" + file.getAbsolutePath());
		System.out.println("name=" + file.getName());
	} else if (file.isDirectory()) {
		String[] filelist = file.list();
		System.out.println("文件个数:"+file.list().length);
		for (int i = 0; i < filelist.length; i++) {
			File readfile = new File(filepath + "\\" + filelist[i]);
			if (!readfile.isDirectory()) {
//					System.out.println("path=" + readfile.getPath());
//					System.out.println("absolutepath=" + readfile.getAbsolutePath());
//					System.out.println("name=" + readfile.getName());
				//文件名称
				String bankName = readfile.getName();
				//文件名称前缀
				String name = bankName.substring(0, bankName.lastIndexOf("."));
				System.out.println("文件名称前缀:"+name);
				//将文件名称转换为拼音
				String nameToSpell = ChineseToSpell.getFullSpell(name);
				System.out.println("文件名称前缀转换为拼音名称名称:"+nameToSpell);
				//要更改的文件名称
				String newName = nameToSpell + bankName.substring(bankName.lastIndexOf("."));
				
				//重命名文件
				renameFile(filepath, name, newName);
			}
		}
	}
//		String file1 = "C:\\Users\\admin\\Desktop\\logo - 副本(2)";
//		String file2 = "C:\\Users\\admin\\Desktop\\logo - 副本(3)";
//		System.out.println(copyFolder(file1, file2));
}

/**
 * 重命名文件
 * @param path    文件夹路径
 * @param oldName 要重命名的文件名称
 * @param newName 重命名后的文件名称
 */
public static void renameFile(String path, String oldName, String newName){
	if(!oldName.equals(newName)){
		File oldFile = new File(path+"/"+oldName);
		File newFile = new File(path+"/"+newName);
		if(newFile.exists()){
			System.out.println(newName+"已经存在");
		} else {
			oldFile.renameTo(newFile);
		}
	}
}

/**
 * 拷贝文件
 * @param file1  要拷贝的文件路径
 * @param file2  要复制到的文件夹目录
 * @return
 */
public static boolean copyFile(String file1, String file2) {
	File in = new File(file1);
	File out = new File(file2);
	//要拷贝的文件
	if (!in.exists()) {
		System.out.println(in.getAbsolutePath() + "要拷贝的文件路径错误!!!");
		return false;
	}
	//目标文件夹
	if (!out.exists()) {
		out.mkdirs();
	}
	FileInputStream fis = null;
	FileOutputStream fos = null;
	try {
		//创建读取 要拷贝的文件
		fis = new FileInputStream(in);
		//创建 要复制到的文件
		fos = new FileOutputStream(new File(file2 + "\\" + in.getName()));
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}
	int c;
	//创建字节数组
	byte[] b = new byte[1024 * 5];
	try {
		//写入文件
		while ((c = fis.read(b)) != -1) {
			fos.write(b, 0, c);
		}
		fis.close();
		fos.flush();
		fos.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
	return false;
}

/**
 * 拷贝文件夹
 * @param file1 要拷贝的文件夹
 * @param file2 拷贝的文件夹
 * @return
 */
public static boolean copyFolder(String file1, String file2) {
	File in = new File(file1);
	File out = new File(file2);
	//要拷贝的文件夹
	if (!in.exists()) {
		System.out.println(in.getAbsolutePath() + "要拷贝的文件夹路径错误!!!");
		return false;
	}
	//目标文件夹
	if (!out.exists()) {
		out.mkdirs();
	}
	//要拷贝的文件夹下的文件或文件夹
	String[] file = in.list();
//		File[] file = in.listFiles();
	FileInputStream fis = null;
	FileOutputStream fos = null;
	File temp = null;
	for (int i = 0; i < file.length; i++) {

		if (file1.endsWith(File.separator)) {
			temp = new File(file1 + file[i]);
		} else {
			temp = new File(file1 + File.separator + file[i]);
		}
		//是文件
		if (temp.isFile()) {
			try {
				//创建读取 要拷贝的文件
				fis = new FileInputStream(temp.getAbsolutePath());
				//创建写入 要拷贝的文件
				fos = new FileOutputStream(new File(file2 + "\\" + temp.getName()));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}else if (temp.isDirectory()) {
			//是文件夹
			copyFolder(temp.getAbsolutePath(), file2 + "\\" +temp.getName());
		}
		int c;
		//创建字节数组
		byte[] b = new byte[1024 * 5];
		try {
			//写入文件
			while ((c = fis.read(b)) != -1) {
				fos.write(b, 0, c);
			}
			fis.close();
			fos.flush();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return false;
}

文件的删除可看:https://blog.csdn.net/qq_40083897/article/details/85159814

汉字转化为拼音见下篇。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值