java输入一个文件夹,查找出所有的文件列表

/*
 *java list file demo
 @author:luowen
 @time:2013-10-25
 * */

import java.io.*;
import java.util.*;

class FileToList
{
    public static void main(String[] args)
    {
           File f = new File("d:/") ;
           List<File> list = new ArrayList<File>();
           toList(f,list);
           toFile(list);
    }

    public static void toList(File f,List<File> list)
    {
        File[] files = f.listFiles();
        for(File file : files)
        {
            if(file.isDirectory())
            {
                toList(file,list);
            }
            else
            {
                if(file.getName().endsWith(".php"))
                list.add(file);
            }
        }
    }
    public static void toFile(List<File> list)
    {
       BufferedWriter bufw  = null;
       try
       {
           bufw = new BufferedWriter(new FileWriter("d:/phpList.txt")) ;
           for(File ls : list)
           {
                bufw.write(ls.getAbsolutePath());
                bufw.newLine();
                bufw.flush();
           }

       }
       catch(IOException e)
       {
             throw new RuntimeException("write error");
       }
       finally
       {
           try
           {
             if(bufw != null)
                bufw.close();
           }
           catch(IOException e)
           {
            throw new RuntimeException("write error");
           }
       }

    }
}

 

posted @ 2013-10-25 23:32  arvim  阅读(383)  评论(0编辑  收藏  举报