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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 唐僧踏歌 中级黑马   /  2014-9-13 21:24  /  1366 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

请问大神,这个程序为什么会出错?谢谢

  1. public class Demo {
  2.         public static void main(String[] args) throws IOException {

  3.                 copy("E:\\javatest", "D:\\java/javadist");
  4.         }

  5.         public static void copy(String reFile, String deFile) throws IOException {
  6.                 File refile = new File(reFile);// 源文件夹
  7.                 File defile = new File(deFile);// 目标空间
  8.                 defile.mkdir();
  9.                 File[] files = refile.listFiles();// 获取的是该文件夹下的全部文件夹和文件。。
  10.                 for (File file : files) {
  11.                         if (file.isDirectory()) {
  12.                                 String path = file.getPath(); // 是目录的话
  13.                                 String newPath = path.replace(reFile, deFile);// 更换路径
  14.                                 File newFile = new File(newPath);// 这里只是有这样一个文件
  15.                                 newFile.mkdir(); // 创建新目录
  16.                                 copy(path, deFile); // 这里利用了递归,把该文件夹下的全部文件打印出来。
  17.                         } else {
  18.                                 String path = file.getPath(); // 不是目录的话

  19.                                 String newPath = path.replace(reFile, deFile);// 更换路径
  20.                                 File newFile = new File(newPath);
  21.                                 FileInputStream fis = new FileInputStream(file);
  22.                                 FileOutputStream fos = new FileOutputStream(newFile);
  23.                                 byte[] buf = new byte[1024];
  24.                                 int len = 0;
  25.                                 while ((len = fis.read(buf)) != -1) {
  26.                                         fos.write(buf, 0, len);
  27.                                 }
  28.                                 if (fis != null)
  29.                                         fis.close();
  30.                                 if (fos != null)
  31.                                         fos.close();
  32.                         }
  33.                 }
  34.         }
  35. }
复制代码

1 个回复

倒序浏览
都没人搭理 ,大神都去上班了?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马