用两个类:
import java.io.*;
class OnlyExt implements FilenameFilter
{
String ext;
public OnlyExt(String ext){
this.ext = "." + ext;
}
public boolean accept(File dir , String name){
return name.endsWith(ext);
}
}class DirListOnly
{
public static void main(String args[]){
String dirname = "/java/applet";
File f1 = new File(dirname);
FilenameFilter only = new OnlyExt("html");
String s[] = f1.list(only); for (int i=0;i<s.length ;i++ )
{
System.out.println(s[i]);
}
}
};
import java.io.*;
class OnlyExt implements FilenameFilter
{
String ext;
public OnlyExt(String ext){
this.ext = "." + ext;
}
public boolean accept(File dir , String name){
return name.endsWith(ext);
}
}class DirListOnly
{
public static void main(String args[]){
String dirname = "/java/applet";
File f1 = new File(dirname);
FilenameFilter only = new OnlyExt("html");
String s[] = f1.list(only); for (int i=0;i<s.length ;i++ )
{
System.out.println(s[i]);
}
}
};
是你的文件目录 在我的机器上有这个目录 D:\java\applet
你自己灵活运用一下就可以了 可以不必写全路径FilenameFilter only = new OnlyExt("html");
"html" 也是同样的道理!!
public boolean accept(File dir,String name){
if (name.endWith(".txt")) return true;
else false;
}
}
String[] files;File dir = new File(directory);
if (!dir.isDirctory())
throw new IllegalArgumentException("No such directory");
files = dir.list(filter);if (files.length() = 0){
System.out.println("There is no .txt file")
}
else{
for (int i := 0,i <= files.length() - 1,i++){
System.out.println(files[i]);
}