- public class IOTest {
- public static void main(String[] args) {
- File file = new File("D:/"); //获得指定路径
- FileFilter fileFilter = new FileFilter("xml"); //文件的后缀名
- String[] files = file.list(fileFilter);
- for(String fileName : files) {
- System.out.println(fileName);
- }
- }
- }
- class FileFilter implements FilenameFilter { //实现FilenameFilter接口
- private String fileExtension;
- public FileFilter(String fileExtension) {
- this.fileExtension = fileExtension;
- }
- public boolean accept(File dir, String name) {
- return name.endsWith("." + fileExtension);
- }
- }