复制指定目录下的指定文件,并修改后缀名

/*
 * 数据源:E:\test\A.java
 * 目的源:E:\test1\A.jad
 */
public class test {
	public static void main(String[] args) throws IOException{
		//封装目录
		File f1=new File("E:\\test");
		File f2=new File("E:\\test1");
		if(!f2.exists()){
			f2.mkdir();
		}
		//获取File文件数组
		//用过滤器把.md文件过滤处理
		File[] ff=f1.listFiles(new FilenameFilter(){

			@Override
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				return new File(dir,name).isFile() && name.endsWith(".md");
			}
			
		});
		//遍历该数组,得到每一个对象
		for(File xx:ff){
			//System.out.println(xx);
			//数据源:E:\test\JDK安装.md
			//目的地:E:\test1\JDK安装.mdd
			String name=xx.getName();
			//System.out.println(name);
			File newFile=new File(f2,name);
			copyFile(xx,newFile);
		}
		
		//在目的地下改名
		File[] dest=f2.listFiles();
		for(File yy:dest){
			String name=yy.getName();
		
			String newName=name.replace(".md",".mdd");
			File ne=new File(f2,newName);
			yy.renameTo(ne);
		}
		
	}

	private static void copyFile(File xx, File newFile) throws IOException{
		// TODO Auto-generated method stub
		BufferedInputStream bis=new BufferedInputStream(new FileInputStream(xx));
		BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(newFile));
		
		int len;
		byte[] bt=new byte[1024];
		while((len=bis.read(bt))!=-1){
			bos.write(bt,0,len);
		}
		
		//释放资源
		bis.close();
		bos.close();
	}

	
	}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java后端指南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值