獲取某個類型文件
阿新 • • 發佈:2018-06-28
目錄 code lena port lean files AR sys lee
package File; import java.io.File; import java.io.FilenameFilter; public class FileNameSelector implements FilenameFilter { String extension = "."; public FileNameSelector(String fileExtensionNoDot) { extension += fileExtensionNoDot; } @Override public boolean accept(File dir, String name) {return name.endsWith(extension); } public static void main(String[] args) { File directory = new File("E:\\"); // 列出所有文件 File[] files = directory.listFiles(); System.out.println("\n目錄" + directory.getName() + "下的所有文件"); for (File file : files) { System.out.print(" " + file.getName()); } // 列出所有.txt文件 File[] txtFiles = directory.listFiles(new FileNameSelector("txt")); System.out.println("\n目錄" + directory.getName() + "下的.txt文件"); for (File file : txtFiles) { System.out.print(" " + file.getName()); }// 列出所有.java文件 File[] javaFiles = directory.listFiles(new FileNameSelector("java")); System.out.println("\n目錄" + directory.getName() + "下的.java文件"); for (File file : javaFiles) { System.out.print(" " + file.getName()); } } }
獲取某個類型文件