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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© chenguoyu520 中级黑马   /  2015-11-8 13:32  /  305 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

计算指定路径下所有.txt文件包括子文件夹里的.txt文件的个数然后将所有的.txt文件复制到D盘下任意目录,请大神修改下我的代码,就.txt文件的个数正确列出,我的代码个数出问题了
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.util.Scanner;


  7. public class Test1 {

  8.         public static void main(String[] args) throws Exception{
  9.                 Scanner sc = new Scanner(System.in);
  10.                 System.out.println("请输入一个路径:");
  11.                 String path = sc.next();
  12.                 sc.close();
  13.                
  14.                 File olddir = new File(path);
  15.                 File newdir = new File("d:\\txt");
  16.                 listTxt(olddir,newdir);
  17.                 renameTxt(newdir);
  18.                
  19.         }
  20.         //列出指定目录中所有文件的方法
  21.         public static void listTxt(File olddir,File newdir) throws Exception{
  22.                 int count = 0;//定义一个计数器,用于计数txt文件个数
  23.                 if(!olddir.exists()){
  24.                         System.out.println("目录不存在");
  25.                 }
  26.                 if(!newdir.exists() || !newdir.isDirectory()){
  27.                         newdir.mkdirs();
  28.                 }
  29.                 File f = new File(newdir.getPath()+"\\"+olddir.getName());
  30.                 f.mkdir();
  31.                 File[] files = olddir.listFiles();
  32.                 for(File file : files){
  33.                         if(file.isDirectory()){
  34.                                 listTxt(file,f);
  35.                                
  36.                         }else if(file.getName().endsWith(".txt")){
  37.                                 count++;
  38.                                 System.out.println("以.txt文件结尾的文件的个数是:"+count);
  39.                                 copyTxt(file,f);
  40.                                
  41.                         }
  42.                 }
  43.         }
  44.         //复制指定目录中文件的方法
  45.         public static void copyTxt(File olddir,File newdir) throws Exception{
  46.                 //创建缓冲区读取流对象并与要复制的文件对象关联
  47.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(olddir));
  48.                 //创建缓冲区写入流对象并与要复制的到的文件对象关联
  49.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newdir.getPath()+"\\"+olddir.getName()));
  50.                 int by = 0;
  51.                 while((by=bis.read())!=-1){
  52.                         bos.write(by);
  53.                 }
  54.                 //关闭资源
  55.                 bis.close();
  56.                 bos.close();
  57.         }
  58.         //修改文件名的方法
  59.         public static void renameTxt(File dir){
  60.                 if(!dir.exists()){
  61.                         System.out.println("文件不存在");
  62.                 }
  63.                 File[] files = dir.listFiles();
  64.                 for(File file : files){
  65.                         if(file.isDirectory()){
  66.                                 renameTxt(file);
  67.                         }else if(file.getName().endsWith(".txt")){
  68.                                 File newfile = new File(file.getPath().replaceAll("\\.txt", ".java"));
  69.                                 file.renameTo(newfile);
  70.                         }
  71.                 }
  72.         }

  73. }
复制代码

2 个回复

倒序浏览
File newdir = new File("d:\\txt");   这就有问题 创建文件夹是File newdir=new File("d://txt")
回复 使用道具 举报
6666666666
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马