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

© 暗影流光 中级黑马   /  2014-7-11 10:30  /  587 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1.         static final String LINE_SEPARATOR = System.getProperty("line.separator");

  2.         /**
  3.          * @param args
  4.          *            获取指定目录下,指定扩展名的文件(包含子目录中的),这些文件的绝对路径写入一个文本文件中
  5.          *            简单说,就是建立一个指定扩展名的文件的列表
  6.          * @throws IOException
  7.          */

  8.         public static void main(String[] args) {
  9.                 List<File> list = new ArrayList<File>();
  10.                 File dir = new File("G:\\bishe");
  11.                 getList(dir, list);
  12.                 outputList(list);
  13.         }

  14.         public static void getList(File dir, List<File> list) {
  15.                 File[] files = dir.listFiles();
  16.                 for (File file : files) {
  17.                         if (file.isDirectory()) {
  18.                                 getList(file, list);
  19.                         } else {
  20.                                 File[] f2 = dir.listFiles(new FilterByName(".doc"));
  21.                                 for (File file2 : f2) {
  22.                                         list.add(file2);
  23.                                 }
  24.                                 break;
  25.                         }
  26.                 }
  27.         }

  28.         public static void outputList(List<File> names) {
  29.                 FileWriter fw = null;
  30.                 try {
  31.                         fw = new FileWriter("E:\\list.txt");
  32.                         for (File file : names) {
  33.                                 String s = file.getAbsolutePath();
  34.                                 fw.write(s);
  35.                                 fw.write(LINE_SEPARATOR);
  36.                         }
  37.                 } catch (IOException e) {
  38.                         e.printStackTrace();
  39.                 } finally {
  40.                         if (fw != null) {
  41.                                 try {
  42.                                         fw.close();
  43.                                 } catch (IOException e) {
  44.                                         e.printStackTrace();
  45.                                 }
  46.                         }
  47.                 }
  48.         }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马