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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

我写了一个复制多级目录下指定文件并改名的程序,虽然可以实现功能但是抛出了空指针异常,大家帮我看看怎么解决
  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. }
复制代码


10 个回复

倒序浏览
  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. }
复制代码
回复 使用道具 举报
回复 使用道具 举报

多了个判断是否存在目标目录,不存在就创建
回复 使用道具 举报
cly成子 来自手机 中级黑马 2015-2-14 21:45:48
报纸
不懂,求高人指点!
回复 使用道具 举报
支持下,混点黑马币
回复 使用道具 举报
回复 使用道具 举报
huangchunwei 来自手机 中级黑马 2015-2-14 23:09:41
8#
看的晕了。。。
回复 使用道具 举报
也在找这道题,收藏下
回复 使用道具 举报
rehan 中级黑马 2015-2-15 09:25:13
10#
空指针异常?那肯定是对象的问题?第一感觉就是对象指向了一个null!!应该是目录或文件问题。。。。
回复 使用道具 举报
Imp_x 高级黑马 2015-2-15 09:32:39
11#
把你else里面的代码换成这个
  1. String str = file.getName;
  2. File newFile = File(destPath,str);
  3. newFile.mkdir();
  4. copyFile(file,newFile);
复制代码


应该看得懂吧{:3_66:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马