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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王晓杰 中级黑马   /  2015-1-11 10:26  /  975 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.*;
  2. import java.util.*;
  3. public class Test10 {

  4.         public static void main(String[]agrs) throws IOException
  5.         {
  6.                 File f = new File("D:");
  7.                 List<File> list = new ArrayList<File>();
  8.                 new Demo001().Demo022(f,list);
  9.                  System.out.println(list.size());
  10.                 new Demo002().cha(list);
  11.         }
  12. }
  13. /*
  14. * 创建一个集合存储 txt文件的名   遍历集合
  15. * 创建一个文件对象
  16. * 遍历这个文件对象
  17. * 查找这个文件对象中的目标文件
  18. * 把目标文件的文件名存入集合中
  19. * 遍历集合 导入的字符流中
  20. *
  21. * 字符流写入TXT文件
  22. *
  23. */

  24. class Demo001
  25. {
  26.         public void  Demo022(File f,List<File> list) throws IOException
  27.         {
  28.                 File [] files = f.listFiles();
  29.                
  30.                 for(int x = 0; x < files.length; x++)
  31.                 {
  32.                         if(files[x].isDirectory())
  33.                         {
  34.                                 new Demo001().Demo022(files[x],list);
  35.                         }else{
  36.                                 if(files[x].getName().endsWith(".java"))
  37.                                 {
  38.                                 list.add(files[x]);
  39.                                 }
  40.                         }
  41.                 }
  42.         }
  43. }

  44. //list中的对象取出来存储到Writer中
  45. class Demo002
  46. {
  47.         public void cha(List<File> list) throws IOException
  48.         {
  49.                 FileWriter fw = new FileWriter("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
  50.                 BufferedWriter bufw = new BufferedWriter(fw);
  51.                 for(File f:list)
  52.                 {
  53.                         String path = f.getAbsolutePath();
  54.                         bufw.write(path);
  55.                         bufw.newLine();
  56.                         bufw.flush();
  57.                 }
  58.                
  59.                 /*File f = null;
  60.                 for(int x = 0; x < list.size(); x++)
  61.                 {
  62.                          f = list[x];
  63.                 }*/
  64.         }
  65.        
  66.        
  67. }
复制代码




4 个回复

正序浏览
new Demo001().Demo022(files[x],list);
这里没有必要创建新对象递归。。
回复 使用道具 举报
  1. package test1;

  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;

  7. /*
  8. * 包含txt的文件名写入txt文本中
  9. *
  10. * */
  11. public class day22 {
  12.         public static void main(String[] args) throws IOException {
  13.                
  14.                 File file = new File("E:\\java11");
  15.                 ArrayList<File> al = new ArrayList<File>();
  16.                
  17.                 File dest = new File("D:\\javainfo.txt");
  18.                
  19.                 add(file,al);//将含有java的文件存入集合中
  20.                
  21.                 list(al,dest);//遍历集合,将文件名写入文本中
  22.                
  23.         }
  24.         private static void list(ArrayList<File> al, File dest) throws IOException {
  25.                 // TODO Auto-generated method stub
  26.                
  27.                 BufferedWriter bufw = new BufferedWriter(new FileWriter(dest));
  28.                
  29.                 for(File f : al)
  30.                 {
  31.                         String path = f.getAbsolutePath();
  32.                         bufw.write(path);
  33.                         bufw.newLine();
  34.                         bufw.flush();
  35.                 }
  36.                
  37.                 bufw.close();
  38.        
  39.         }

  40.         private static void add(File file, ArrayList<File> al) {
  41.                 // TODO Auto-generated method stub
  42.                 File[] files = file.listFiles();
  43.                 for(File f :files)
  44.                 {
  45.                         if(f.isDirectory())
  46.                         {
  47.                                 add(f,al);
  48.                         }else{
  49.                                 if(f.getName().endsWith(".java"))
  50.                                         al.add(f);
  51.                                
  52.                         }
  53.                        

  54.                 }
  55.                
  56.         }

  57. }
复制代码


我写了一个,你参考一下。
回复 使用道具 举报
你起码把报的错误说一下啊,而且你这个名称看起来很累
回复 使用道具 举报
帮你顶一下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马