FileFilterです。
2008/08/31
java
Javaです。
ファイルとディレクトリの振り分け
あと、更新時間でソート。
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
public class TestFileList {
public static void main(String[] args) {
File[] filesDir = new File("c:\\tmp\\").listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
for (File file : filesDir) {
System.out.println(file + ": "
+ new Date(file.lastModified()).toLocaleString());
}
System.out.println("===========================");
System.out.println("===========================");
System.out.println("===========================");
System.out.println("===========================");
File[] files = new File("c:\\tmp\\").listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return !pathname.isDirectory();
}
});
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return new Long(o1.lastModified()).compareTo(new Long(o2
.lastModified()));
}
});
for (File file : files) {
System.out.println(file + ": "
+ new Date(file.lastModified()).toLocaleString());
}
}
}
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
public class TestFileList {
public static void main(String[] args) {
File[] filesDir = new File("c:\\tmp\\").listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
for (File file : filesDir) {
System.out.println(file + ": "
+ new Date(file.lastModified()).toLocaleString());
}
System.out.println("===========================");
System.out.println("===========================");
System.out.println("===========================");
System.out.println("===========================");
File[] files = new File("c:\\tmp\\").listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return !pathname.isDirectory();
}
});
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return new Long(o1.lastModified()).compareTo(new Long(o2
.lastModified()));
}
});
for (File file : files) {
System.out.println(file + ": "
+ new Date(file.lastModified()).toLocaleString());
}
}
}
: