本帖最后由 夏振博 于 2013-2-8 00:56 编辑
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class Test {
- public static void main(String[] args) {
- File dir = new File("D:\\");
- List<File> list = new ArrayList<File>();
- fileoList(dir, list);
- File file = new File("a.txt");
- listToFile(list, file.toString());
- }
- public static void fileoList(File dir, List<File> list) {
- File[] files = dir.listFiles();
- if (files != null) {//只加了这里,如果不判断会报空指针异常,直接盘符下有不允许访问的文件,当访问时会返回null
- for (File file : files) {
- if (file.isDirectory())
- fileoList(file, list);
- else {
- if (file.getName().endsWith(".java"))
- list.add(file);
- }
- }
- }
- }
- public static void listToFile(List<File> list, String filePath) {
- BufferedWriter bufw = null;
- try {
- bufw = new BufferedWriter(new FileWriter(filePath));
- for (File paths : list) {
- String path = paths.getAbsolutePath();
- bufw.write(path);
- bufw.flush();
- bufw.newLine();
- }
- } catch (IOException e) {
- throw new RuntimeException("读取数据失败");
- } finally {
- try {
- if (bufw != null)
- bufw.close();
- } catch (Exception e) {
- throw new RuntimeException("写入流关闭失败");
- }
- }
- }
- }
- 想问下,难道在代码文字模式下无法改变字体颜色吗,???
复制代码 |