用到的API:
File的構造函數:
list的兩種方法,注意FilenameFilter的使用:
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]);
??????????? }
??????? }
??? }
}
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]);
??????????? }
??????? }
??? }
}