本帖最后由 杨增坤 于 2013-9-9 19:55 编辑
- <p><p>import java.util.*;
- import java.io.*;
- class JavaFileList
- {
- public static void main(String[] args)
- {
- File dir=new File("e:\\java try");
- List<File> list=new ArrayList<File>();
- fileToList(dir,list);
- System.out.println(list.size());
-
- }
- public static void fileToList(File dir,List<File> list)
- {
- File[] files = dir.listFiles();
- for(File file:files)
- {
- if(file.isDirectory())
- fileToList(file,list);
- else
- {
- if(file.getName().endsWith(".java"))
- list.add(file);
- }
- }
- }
- public static void writerToFile(List<File> list,String javaListFile)throw IOException
- {
- BufferedWriter bufw = null;
- try
- {
- bufw = new BufferedWriter(new FileWriter(javaListFile));
- for(File f:list)
- {
- String path = f.getAbsolutePath();
- bufw.writer(path);
- bufw.newLine();
- bufw.flush;
- }</p><p> }
- catch(IOException e)
- {
- throw e;
- }
- finally
- {
- try
- {
- if(bufw!=null)//这里为什么要加这句呢???突然就不明白了
- bufw.close();
- }
- catch (IOException e)
- {
- throw e;
- }
- }
- }</p></p>
复制代码 |