|
常用链接
留言簿(4)
随笔档案(16)
相册
最新随笔
积分与排名
最新评论
阅读排行榜
评论排行榜
Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
用到的API:
static
String
|
separator
The system-dependent default name-separator character, represented as a string for convenience.
|
File的构造函数:
File
(
File
parent,
String
child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
|
File
(
String
pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
|
File
(
String
parent,
String
child)
Creates a new File instance from a parent pathname string and a child pathname string.
|
list的两种方法,注意FilenameFilter的使用:
String
[]
|
list
()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
|
String
[]
|
list
(
FilenameFilter
filter)
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
|
boolean
|
isDirectory
()
Tests whether the file denoted by this abstract pathname is a directory.
|
import java.io.File; import java.io.FilenameFilter; public class Main { public static void main(String[] args) { File fdir=new File(File.separator);//获取当前应用程序所在的根目录 showName(fdir,"Program Files");//因为我的这个程序在D盘运行,所以是搜索d:/Program Files目录 } public static void showName(File fdir,String s){//递归搜索,遇到是目录而不是文件再把目录传进去搜索 File f=new File(fdir,s); String[] sf=f.list(new FilenameFilter(){//使用匿名内部类重写accept方法 public boolean accept(File dir, String name){ if(new File(dir,name).isDirectory()){ return true; } return name.indexOf(".txt")!=-1 || name.indexOf(".wav")!=-1;//筛选出.txt和.wav文件 } }); for(int i=0;i<sf.length;i++){ if(new File(f,sf[i]).isDirectory()){//判断是目录还是文件 showName(f,sf[i]); }else{ System.out.println(sf[i]); } } } }
|
|