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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 天蝎降降温 中级黑马   /  2014-10-20 12:14  /  661 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class Test7 {

  2.         public static void main(String[] args) throws IOException {                  
  3.            String s1 = "c://1";
  4.            String s2 = "d://1";
  5.            copyF(s1,s2);
  6.            
  7.         }
  8.         public static void copyF(String oldpath,String newpath) throws IOException{
  9.                    File f1 = new File(newpath);
  10.                    if(!f1.exists())
  11.                    {                          
  12.                    f1.mkdirs();
  13.                    }
  14.                    File f2 = new File(oldpath);
  15.                    File[] fi = f2.listFiles();
  16.                    //File temp = null;
  17.                    for(File f: fi){
  18.                            if(f.isDirectory()){
  19.                                    File f3 = new File(f1,f.getName());
  20.                                    f3.mkdirs();
  21.                                    copyF(f.getAbsolutePath(),f3.getAbsolutePath());
  22.                                    //temp = new File(oldpath+File.separator+f.getName());
  23.                                   // f1 = new File(temp.getName());
  24.                                   // copyF(temp.getAbsolutePath(),f1.getAbsolutePath());
  25.                            }
  26.                            if(f.isFile()){
  27.                                    BufferedInputStream bi =new BufferedInputStream
  28.                                    (new FileInputStream(f));
  29.                                    BufferedOutputStream bo = new BufferedOutputStream
  30.                                    (new FileOutputStream(f1.getAbsolutePath()+File.separator+f.getName()));
  31.                                    int i =0;
  32.                                    while((i=bi.read())!=-1){
  33.                                             bo.write(i);
  34.                                             bo.flush();
  35.                                    }
  36.                                    bi.close();
  37.                                    bo.close();
  38.                            }
  39.                           
  40.                    }
  41.         }

  42. }
复制代码
我这个复制多级文件夹有什么需要改进的地方,成是成功了,但我发现有点点别扭。


评分

参与人数 1黑马币 +1 收起 理由
杨佳名 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马