| 
 
| import java.io.*; class Test9
 {
 public static void main(String[] args) throws IOException
 {
 File src=new File("D:\\JAVA_基础班");//拷贝目录
 File dest=new File("D:\\新建文件夹");//复制目录
 copyTo(src,dest);
 
 }
 public static void copyTo(File src,File dest) throws IOException
 {
 
 File[] files=src.listFiles();
 String str1=src.toString().replaceAll("\\\\","\\\\\\\\");//目录字符串转义
 String str2=dest.toString().replaceAll("\\\\","\\\\\\\\");//目录字符串转义
 if(files==null)
 return;
 for(File file:files)
 {
 if(file.isDirectory())//是目录继续递归
 {
 copyTo(file,dest);
 }
 else if(file.isFile() &&file.toString().endsWith(".java"))//不是目录,切是文件,结尾为java开始复制
 {
 String scc=file.getName().toString();
 String ds=scc.substring(0,scc.length()-5);
 ds=ds+".txt";
 BufferedInputStream bfis=new BufferedInputStream(new FileInputStream(file));//读取文件
 BufferedOutputStream bfos=new BufferedOutputStream(new FileOutputStream(new File(str2,ds))); //重新写入,新地址目录+file的文件名
 byte [] b=new byte[1024];
 int len=-1;
 while((len=bfis.read(b))!=-1)
 {
 bfos.write(b,0,len);
 
 }
 bfis.close();
 bfos.close();
 }
 }
 }
 }
 
 
 
 | 
 |