黑马程序员技术交流社区

标题: 求指导。 [打印本页]

作者: 天蝎降降温    时间: 2014-10-20 12:14
标题: 求指导。
  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. }
复制代码
我这个复制多级文件夹有什么需要改进的地方,成是成功了,但我发现有点点别扭。







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2