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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 默默丶 高级黑马   /  2014-10-14 19:22  /  828 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1.         import java.io.*;
  2. public class Demo11 {
  3.         static String sourcePath = "D:\\";
  4.         static String aimPath = "E:\\hahaha";
  5.         public static void main(String[] args)throws Exception{
  6.                 File file = new File(sourcePath);
  7.                 listFile(file);
  8.         }
  9.         private static void listFile(File file)throws Exception{
  10.                
  11.                 File[] files = file.listFiles();
  12.                 for(int x = 0;x<files.length;x++){
  13.                         if(files[x].isDirectory())
  14.                                 listFile(files[x]);
  15.                         if(files[x].getName().endsWith(".java"))
  16.                                 copy(files[x]);
  17.                 }
  18.         }
  19.         private static void copy(File file)throws Exception{
  20.                 String pathf =file.getPath();
  21.                 String paths = pathf.replace(sourcePath, "");
  22.                 String path = paths.replace(".java", ".txt");
  23.                 FileInputStream fis = new FileInputStream(pathf);
  24.                 FileOutputStream fos = new FileOutputStream(aimPath+File.separator+path);
  25.                
  26.                 byte[] buf = new byte[1024];
  27.                 int len = 0;
  28.                 while((len = fis.read(buf))!=-1){
  29.                         fos.write(buf,0,len);
  30.                 }
  31.                 fis.close();
  32.                 fos.close();
  33.         }
  34. }
复制代码

2 个回复

倒序浏览
原来是我定义的sourcePath出错了
回复 使用道具 举报
为什么要sourcePath定义为文件夹名字没事儿啊 为什么直接成为ABCDE盘最后都会报空指针异常
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马