分类:
2008-09-09 16:48:57
import java.io.File;
import java.util.*;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-7-18 22:02:30
* Note: 模拟dir测试的例程
*/
public class TestFile {
public static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
int nl = 18;
public static void main(String args[]) {
test();
}
public static void test() {
System.out.println("----------test()----------");
File file = new File("F:\\SOFT_DEV");
// file.isDirectory();
// System.out.println(File.pathSeparator);
// System.out.println(File.pathSeparatorChar);
// System.out.println(File.separator);
// System.out.println(File.separatorChar);
FileComparator fc = new FileComparator();
File[] lf = file.listFiles();
Arrays.sort(lf, fc); //数组排序
for (File f : lf) {
String dataStr = df.format(new Date(f.lastModified()));
if (f.isDirectory()) {
System.out.print(dataStr + "
} else {
System.out.print(dataStr + " " + getFormatString(String.valueOf(NumberFormat.getInstance().format(f.length()))));
}
System.out.println(" " + f.getName());
}
}
//格式化函数
public static String getFormatString(String s) {
int nb = 12 - s.length();
StringBuilder sb = new StringBuilder();
if (nb > 0) {
for (int n = 1; n <= nb; n++) {
sb = sb.append(" ");
}
}
return sb.append(s).toString();
}
}