一个文件夹下有多个文件夹和EXCEL文件(而且后缀名还可能会隐藏了),用程序怎样只取得当前文件夹下的所有excel文件?
------解决方案--------------------
import java.io.File;
import java.io.FileFilter;
public class PictureFileFilter implements FileFilter {
private final String EXCEL = "xls";
public boolean accept(File pathname) {
if (pathname == null){
return false;
}
String filePath = pathname.getPath();
int point = filePath.lastIndexOf('.');
if (point == -1){
return false;
}
String postfix = filePath.substring(point+1);
if (postfix.equals(EXCEL)){
return true;
}
return false;
}
}
public class TestFile {
public void test(String filePath) {
File file = new File(filePath);
System.out.println(file.getName());
for (File result : file.listFiles(new PictureFileFilter())) {
System.out.println(result.getName());
}
}
public static void main(String[] args) {
new TestFile().test(args[0]);
}
}
------解决方案--------------------
- Java code
import java.io.File;class T { public static void main(String[] args) { File file = new File("path"); File[] fileList = file.listFiles(); for (File f:fileList){ if (f.isFile() && (f.getName().endsWith(".xls") || f.getName().endsWith(".xlsx"))){ System.out.println(); } } }}