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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

我也要面试了,要疯狂做题啊
  1. import java.io.*;

  2. class CopyDir
  3. {
  4.         public static void main(String[] args) throws Exception
  5.         {
  6.                 File filescr = new File("F:\\03java\\1016");
  7.                 File filedes = new File("E:\\");

  8.                 copy(filescr,filedes);
  9.         }

  10.         public static void copy(File filescr,File filedes) throws Exception
  11.         {
  12.        
  13.                         String dirName = filescr.getName();
  14.                         filedes = new File(filedes.getAbsolutePath()+"\\"+dirName);
  15.                         filedes.mkdir();
  16.                         File[] files = filescr.listFiles();
  17.                         for(File file:files)
  18.                         {
  19.                                 if(file.isDirectory())
  20.                                 {
  21.                                         copy(file,filedes);
  22.                                 }
  23.                                 else
  24.                                 {
  25.                                         BufferedInputStream bis =
  26.                                                 new BufferedInputStream(new FileInputStream(file));
  27.                                         String fileName = file.getName();
  28.                                         File filedes1 = new File(filedes.getAbsolutePath()+"\\"+fileName);
  29.                                         BufferedOutputStream bos =
  30.                                                 new BufferedOutputStream(new FileOutputStream(filedes1));
  31.                                         int len=0;
  32.                                         while((len=bis.read())!=-1)
  33.                                         {
  34.                                                 bos.write(len);
  35.                                                 bos.flush();
  36.                                         }
  37.                                         bis.close();
  38.                                         bos.close();

  39.                                 }
  40.                         }
  41.         }
  42. }
复制代码



评分

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

查看全部评分

2 个回复

倒序浏览
楼主给力
回复 使用道具 举报
不错 祝楼主好运
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马