A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 郑飞 高级黑马   /  2014-10-20 10:31  /  1219 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.*;
  2. public class CopyDir {
  3.         /**
  4.          * 将"E:\\MyEclipse 10\\io\\src\\files";下的"copyDir文件夹"复制到"E:\\java\\workspace";
  5.          * 把.java文件改成.txt文件
  6.          */
  7.         private static final String P = File.separator;//路径分隔符
  8.        
  9.         public static void main(String[] args)throws IOException
  10.         {
  11.                 String dest = "E:\\java\\workspace";
  12.                 String src = "E:\\MyEclipse 10\\io\\src\\files";
  13.                 String name = "copyDir";
  14.                 copyDir(dest,src,name,new MyFilenameFilter());
  15.         }
  16.        
  17.         //拷贝单个文件
  18.         public static void copy(String dest,String src)throws IOException
  19.         {
  20.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
  21.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
  22.                 int ch = -1;
  23.                 while((ch = bis.read())!=-1)
  24.                 {
  25.                         bos.write((byte)ch);
  26.                 }
  27.                 bis.close();
  28.                 bos.close();
  29.         }
  30.        
  31.         //判断arr数组里是否包含t元素;用于区分.java文件和其他文件
  32.         public static <T> boolean isContains(T t , T[] arr)
  33.         {
  34.                 for(int i = 0;i<arr.length;i++)
  35.                 {
  36.                         if(t.equals(arr[i]))
  37.                                 return true;
  38.                 }
  39.                 return false;
  40.         }
  41.        
  42.         //建立多级文件夹
  43.         public static void mkdirs(String dest)
  44.         {
  45.                 new File(dest).mkdirs();
  46.         }
  47.         //复制文件夹
  48.         public static void copyDir(String dest,String src,String name,MyFilenameFilter mft)throws IOException
  49.         {
  50.                 //判断源文件夹是否存在;
  51.                 File df = new File(dest+P+name);
  52.                 if(!df.exists())
  53.                         mkdirs(dest+P+name);//不存在则建立该文件夹
  54.                 File sf = new File(src+P+name);
  55.                 File[] files = sf.listFiles();//该文件夹下所有文件组成的数组
  56.                 File[] javafiles = sf.listFiles(mft);//该文件夹下所有.java文件组成的数组
  57.                 for(int i = 0 ; i<files.length;i++)
  58.                 {
  59.                         File file = files[i];
  60.                         String oldname = file.getName();
  61.                         String newname = null;
  62.                         if(file.isDirectory())//如果是文件夹,先建立,然后进入递归;
  63.                         {
  64.                                 mkdirs(dest+P+name+P+oldname);
  65.                                 copyDir(dest+P+name,src+P+name,oldname,mft);
  66.                         }
  67.                         else if(file.isFile())//是文件的情况
  68.                         {
  69.                                 if(isContains(file,javafiles))//该文件是.java文件
  70.                                          newname = oldname.split(".java")[0]+".txt";//该成新文件名
  71.                                 else
  72.                                         newname = oldname;//不是.java文件,文件名不变
  73.                                 copy(dest+P+name+P+newname,src+P+name+P+oldname);//最后复制该文件;
  74.                         }
  75.                 }
  76.         }
  77. }
  78. //用于过滤.java文件
  79. class MyFilenameFilter implements FilenameFilter
  80. {
  81.         public boolean accept(File dir,String filename)
  82.         {
  83.                 return filename.endsWith(".java");
  84.         }
  85. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

5 个回复

倒序浏览
卢仪敏 发表于 2014-10-20 10:45
看你好久了,还没进黑马吗

快了 事情耽误了点时间
最近有打算面试 不过超龄 压力好大:L
回复 使用道具 举报
郑飞 发表于 2014-10-20 11:58
快了 事情耽误了点时间
最近有打算面试 不过超龄 压力好大

你进了没 哪期的
我是希望能在51期之前进黑马
回复 使用道具 举报
卢仪敏 发表于 2014-10-20 10:45
看你好久了,还没进黑马吗

嗯 谢啦:handshake
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马