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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

©   /  2013-2-7 10:50  /  1836 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 夏振博 于 2013-2-8 00:56 编辑
  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;

  7. public class Test {
  8.         public static void main(String[] args) {
  9.                 File dir = new File("D:\\");
  10.                 List<File> list = new ArrayList<File>();
  11.                 fileoList(dir, list);

  12.                 File file = new File("a.txt");
  13.                 listToFile(list, file.toString());
  14.         }

  15.         public static void fileoList(File dir, List<File> list) {
  16.                 File[] files = dir.listFiles();
  17.                 if (files != null) {//只加了这里,如果不判断会报空指针异常,直接盘符下有不允许访问的文件,当访问时会返回null
  18.                         for (File file : files) {
  19.                                 if (file.isDirectory())
  20.                                         fileoList(file, list);
  21.                                 else {
  22.                                         if (file.getName().endsWith(".java"))
  23.                                                 list.add(file);
  24.                                 }
  25.                         }
  26.                 }
  27.         }

  28.         public static void listToFile(List<File> list, String filePath) {
  29.                 BufferedWriter bufw = null;

  30.                 try {
  31.                         bufw = new BufferedWriter(new FileWriter(filePath));
  32.                         for (File paths : list) {
  33.                                 String path = paths.getAbsolutePath();
  34.                                 bufw.write(path);
  35.                                 bufw.flush();
  36.                                 bufw.newLine();
  37.                         }
  38.                 } catch (IOException e) {
  39.                         throw new RuntimeException("读取数据失败");
  40.                 } finally {
  41.                         try {
  42.                                 if (bufw != null)
  43.                                         bufw.close();
  44.                         } catch (Exception e) {
  45.                                 throw new RuntimeException("写入流关闭失败");
  46.                         }
  47.                 }

  48.         }
  49. }
  50. 想问下,难道在代码文字模式下无法改变字体颜色吗,???
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马