打印某个目录下所有文件的名字
public class Test {
public static void main(String[] args) {
File file = new File("C:\\cangku");
new Test().listDirs(file);
}
// 打印某个目录下所有文件的名字
public void listDirs(File file) {
if (file.exists()) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File file1 : files) {
listDirs(file1);
}
} else {
System.out.println(file.getAbsolutePath());
}
} else {
System.out.println("文件不存在");
}
}
}
过滤文件夹以endwith结尾的文件
public class MyFileFilter implements FileFilter {
String endWith;
public MyFileFilter(String endWith) {
this.endWith = endWith;
}
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(endWith);
}
}
public class Test {
public static void main(String[] args) {
File file = new File("C:\\cangku");
File[] files = file.listFiles(new MyFileFilter("A"));
for (File file1 : files) {
System.out.println(file1);
}
}
}
网友评论