Java判断文件类型
package com.ycy1;
import java.io.File;
import java.io.FilenameFilter;
public class FilenameFilterImpl implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
File file = new File(dir, name);
boolean isFile = file.isFile();
if(isFile) {
String fileName = file.getName();
if(fileName.endsWith(".txt")) {
return true;
}else {
return false;
}
}
return false;
}
}
package com.ycy1;
import java.io.File;
import java.util.Arrays;
public class Test04 {
public static void main(String[] args) {
File file = new File("E:" + File.separator + "test");
FilenameFilterImpl filenameFilterImpl = new FilenameFilterImpl();
String[] list = file.list(filenameFilterImpl);
String string = Arrays.toString(list);
System.out.println(string);
}
}