复制指定目录下的指定java文件到新的文件夹,并修改后缀名为.txt文件。


/*
 * 4、复制指定目录下的指定java文件到新的文件夹,并修改后缀名为.txt文件。
 * 1.封装目标路径
 * 2.确定新路径
 * 3.遍历判断目标路径的目标文件
 * 4.copy 目标文件到 新的 路径里
 * 5.目标更名
 * 返回值类型 :
 * 参数列表:
 * 
 */

public class IoTest04 {
		public static void main(String[] args) {
			//copy ("src/iotestdemo/IoTest01.java","q.txt");///day20demo/src/iotestdemo/IoTest01.java
			//1.封装目标路径
			File srcFileName = new File("D:\\JAVA\\百战程序员\\JavaSE\\02_JavaSE\\day18\\04_代码\\123456");//D:\JAVA\百战程序员\JavaSE\02_JavaSE\day18\04_代码\123456
			//2.确定新路径
			File loadingFilename = new File("D:\\111");
			if ( ! loadingFilename.exists()) {
				loadingFilename.mkdir();
			}
			//3.遍历判断获取目标路径的目标文件
			File[] files = srcFileName.listFiles(new FileFilter() {
				@Override
				public boolean accept(File ss) {
					return ss.isFile() && ss.getName().endsWith(".java");
				}
			});
			//4.copy 目标文件到 新的 路径里
			for (File file : files) {
				File newFile = new File(loadingFilename,file.getName());
				copy(file, newFile);
			}
			//5.目标更名
			File[] file = loadingFilename.listFiles();
			if (file != null && file.length != 0) {
				for (File file2 : file) {
					String newFileName = file2.getName().replaceAll(".java", ".txt");
					File newFile = new File(loadingFilename,newFileName);
					file2.renameTo(newFile);
				}
			}
		}

		private static void copy(File srcFileName, File loadingFilename) {
			try (BufferedReader bif = new BufferedReader(new FileReader(srcFileName));
					BufferedWriter bos = new BufferedWriter(new FileWriter(loadingFilename));) {
				
				//int len = 0;
				//byte[] bys = new byte[1024];
				String line = null;
				while ((line = bif.readLine()) != null) {
					bos.write(line);
					bos.newLine();
					bos.flush();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值