- package FileDemo;
- import java.io.*;
- import java.util.*;
- public class JavaFileList {
- public static void main(String[] args) throws Exception{
- File dir = new File("D:\\");
- List<File> list = new ArrayList<File>();
- fileToList(dir,list);
- File file = new File(dir, "javalist.txt");
- writeToFile(list, file.toString());
- }
- public static void fileToList(File dir, List<File> list) {
- File[] files = dir.listFiles();
- for(int x=0;x<files.length;x++){
- if(files[x].isDirectory())
- fileToList(files[x], list);
- else if(files[x].getName().endsWith(".java")){
- list.add(files[x]);
- }
- }
- }
- public static void writeToFile(List<File> list, String javaListFile) throws Exception{
- FileWriter fw = new FileWriter(javaListFile);
- BufferedWriter bufw = new BufferedWriter(fw);
- for(File f : list){
- String path = f.getAbsolutePath();
- System.out.println(path);
- bufw.write(path);
- bufw.newLine();
- bufw.flush();
- }
- }
- }
复制代码
Exception in thread "main" java.lang.NullPointerException
at FileDemo.JavaFileList.fileToList(JavaFileList.java:17)
at FileDemo.JavaFileList.fileToList(JavaFileList.java:19)
at FileDemo.JavaFileList.main(JavaFileList.java:10)
|
|