现在的位置: 首页 > 综合 > 正文

目录下的文件和目录输出

2013年07月02日 ⁄ 综合 ⁄ 共 1138字 ⁄ 字号 评论关闭

import java.io.*;
public class test
{
 public void getDir(String strPath) 
 { 
  File f=new File(strPath); 
 if(f.isDirectory()) 
 { 
  System.out.println(f.getAbsolutePath()); //输出目录下所有目录
  File[] fList=f.listFiles(); 
 for(int j=0;j <fList.length;j++) 
          { 
 if(fList[j].isDirectory()) 
 { getDir(fList[j].getPath()); 

}
        // System.out.println(fList[j].getAbsolutePath()); //输出目录下所有文件
  } 
 }  
 }
    public static void main(String args[]) throws Exception
    {
     test ts=new test();
        ts.getDir("E://project");
    }
}

 过滤器版:

 

public class test
{
public static void main(String args[]) throws Exception
    {
        File aaa = new File("c://");
        aaa.mkdir();
        MyFileFilter mff = new MyFileFilter(true);
         File[] fileNames = aaa.listFiles(mff);
        for(int i = 0;i<fileNames.length;i++)
        {
            System.out.println(fileNames[i].getAbsolutePath());
        }
       
    }
}
class MyFileFilter implements FileFilter
{
    private boolean isDirectory;
    public MyFileFilter(boolean isDir)
    {
        this.isDirectory = isDir;
    }
    public boolean accept(File pathname)
    {
        if(pathname.isDirectory() == isDirectory)
        {
            return true;
        }
        else
            return false;
    }

}

抱歉!评论已关闭.