初學Java,IO之檔案過濾器(四十)
阿新 • • 發佈:2019-02-19
FilenameFilter介面包含accpet 方法,該方法將依次對指定File的所有子目錄,子資料夾進行迭代,如果該方法返回true,則list方法會列出該子目錄或者子資料夾
import java.io.*; class MyFileNameFilter implements FilenameFilter { public boolean accept(File dir,String name) { //如果檔案以.java結尾,或者檔案對應一個路徑,返回true return name.endsWith(".java") || new File(name).isDirectory(); } }
public class FileNameFilterTest
{
public static void main(String[] args)
{
File file = new File(".");
String[] nameList = file.list(new MyFileNameFilter());
for(String name : nameList)
{
System.out.println(name);
}
}
}
輸出:Apple.java
CreateInnerInstance.java
DiscernVariable.java
FileNameFilterTest.java
FileTest.java
Gender.java
GenderDesc.java
Leaf.java
LocalInnerClass.java
Mid.java
MyFileNameFilter.java
old files
Operation.java
Out.java
Person.java
PrintObject.java
Product.java
Root.java
Singleton.java
StringTest.java
SubClass.java
template.java
Test.java
TestAnonymous.java
TestCollection.java
TestGender.java
TestScannerKeyBoard.java
TestSingleton.java
TestSynchronized.java
TestToString.java
TestWeakHashMap.java