/*
* 数据源: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();
}
}
复制指定目录下的指定文件,并修改后缀名
最新推荐文章于 2023-04-13 23:41:30 发布