黑马程序员技术交流社区

标题: 关于复制多级目录下指定文件并改名的问题 [打印本页]

作者: 白衣布士    时间: 2015-2-14 14:36
标题: 关于复制多级目录下指定文件并改名的问题
我写了一个复制多级目录下指定文件并改名的程序,虽然可以实现功能但是抛出了空指针异常,大家帮我看看怎么解决
  1. /**
  2. * 需求:复制多级目录下指定文件并改名
  3. */
  4. public class CopyFile {
  5.         public static void main(String[] args) throws IOException {
  6.                 // 需要复制的目录
  7.                 String srcPath = "D:\\aaa";
  8.                 // 目标目录
  9.                 String destPath = "E:\\AAA";
  10.                 copyFile(srcPath, destPath);
  11.         }

  12.         public static void copyFile(String srcPath, String destPath)
  13.                         throws IOException {
  14.                 // 封装源目录,目标目录
  15.                 File srcFile = new File(srcPath);
  16.                 File destFile = new File(destPath);

  17.                 File[] fileArray = srcFile.listFiles();
  18.                 // 判断目标目录是否存在
  19.                 if (!destFile.exists()) {
  20.                         destFile.mkdir();
  21.                 }

  22.                 for (File file : fileArray) {

  23.                         if (file.isFile() && file.getName().endsWith(".java")) {
  24.                                 // 改名
  25.                                 String newName = file.getName().replace(".java", ".jad");
  26.                                 File newFile = new File(destFile, newName);

  27.                                 BufferedReader br = new BufferedReader(new FileReader(file));
  28.                                 BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));

  29.                                 String str = null;
  30.                                 while ((str = br.readLine()) != null) {
  31.                                         bw.write(str);
  32.                                         bw.newLine();
  33.                                         bw.flush();
  34.                                 }
  35.                                 bw.close();
  36.                                 br.close();
  37.                         } else {
  38.                                 // 返回该目录的绝对路径,做为源目录传入
  39.                                 String newSrcPath = file.getAbsolutePath();
  40.                                 copyFile(newSrcPath, destPath);
  41.                         }

  42.                 }

  43.         }
  44. }
复制代码



作者: 万合天宜    时间: 2015-2-14 18:05
  1. /*
  2. * 需求:复制多极文件夹
  3. *
  4. * 数据源:E:\\aaa
  5. * 目的地:E:\\AAA
  6. *
  7. * 分析:
  8. *                 A:封装数据源File
  9. *                 B:封装目的地File
  10. *                 C:判断该File是文件夹还是文件
  11. *                         a:是文件夹
  12. *                                 就在目的地目录下创建该文件夹
  13. *                                 获取该File对象下的所有文件或者文件夹File对象
  14. *                                 遍历得到每一个File对象
  15. *                                 回到C
  16. *                         b:是文件
  17. *                                 就复制(字节流)并改名字
  18. */
  19. public class CopyFolders {
  20.         public static void main(String[] args) throws IOException {
  21.                 // 封装数据源File
  22.                 File srcFile = new File("E:\\aaa");
  23.                 // 封装目的地File
  24.                 File destFile = new File("E:\\AAA");

  25.                 // 复制文件夹的功能
  26.                 copyFolder(srcFile, destFile);
  27.         }

  28.         private static void copyFolder(File srcFile, File destFile)
  29.                         throws IOException {
  30.                 // 判断该File是文件夹还是文件
  31.                 if (srcFile.isDirectory()) {
  32.                         // 文件夹
  33.                         File newFolder = new File(destFile, srcFile.getName());
  34.                         newFolder.mkdir();

  35.                         // 获取该File对象下的所有文件或者文件夹File对象
  36.                         File[] fileArray = srcFile.listFiles();
  37.                         for (File file : fileArray) {
  38.                                 copyFolder(file, newFolder);
  39.                         }
  40.                 } else {
  41.                         // 文件
  42.                         File newFile = new File(destFile, srcFile.getName());
  43.                         copyFile(srcFile, newFile);
  44.                 }
  45.         }

  46.         private static void copyFile(File srcFile, File newFile) throws IOException {
  47.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
  48.                                 srcFile));
  49.                 BufferedOutputStream bos = new BufferedOutputStream(
  50.                                 new FileOutputStream(newFile));

  51.                 byte[] bys = new byte[1024];
  52.                 int len = 0;
  53.                 while ((len = bis.read(bys)) != -1) {
  54.                         bos.write(bys, 0, len);
  55.                 }

  56.                 bos.close();
  57.                 bis.close();
  58.         }
  59. }
复制代码

作者: 白衣布士    时间: 2015-2-14 21:15
万合天宜 发表于 2015-2-14 18:05

好像也抛异常了
作者: 白衣布士    时间: 2015-2-14 21:21
万合天宜 发表于 2015-2-14 18:05

多了个判断是否存在目标目录,不存在就创建
作者: cly成子    时间: 2015-2-14 21:45
不懂,求高人指点!
作者: 刘立峰    时间: 2015-2-14 22:14
支持下,混点黑马币
作者: 你的微笑很美    时间: 2015-2-14 22:40
万合天宜 发表于 2015-2-14 18:05

好复杂的说
作者: huangchunwei    时间: 2015-2-14 23:09
看的晕了。。。
作者: 枪杆不如笔杆    时间: 2015-2-15 09:13
也在找这道题,收藏下
作者: rehan    时间: 2015-2-15 09:25
空指针异常?那肯定是对象的问题?第一感觉就是对象指向了一个null!!应该是目录或文件问题。。。。
作者: Imp_x    时间: 2015-2-15 09:32
把你else里面的代码换成这个
  1. String str = file.getName;
  2. File newFile = File(destPath,str);
  3. newFile.mkdir();
  4. copyFile(file,newFile);
复制代码


应该看得懂吧{:3_66:}




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