点击这里给我发消息 点击这里给我发消息

Java实现Windows的dir命令

添加时间:2013-12-7
    相关阅读: Windows 系统

        为做一个文件管理系统所实现的一个很粗糙的小例子,感觉还是有研究价值的。

        下面是源代码:

        import java.util.Comparator;
        import java.io.File;
        /**
        * Created by IntelliJ IDEA.<br>
        * <b>User</b>: leizhimin<br>
        * <b>Date</b>: 2008-7-18 22:43:44<br>
        * <b>Note</b>: 文件排序,按照先目录后文件方式排
        */
        public class FileComparator implements Comparator<File> {

            public int compare(File o1, File o2) {
                if (o1.isDirectory() && o2.isDirectory()) return o1.compareTo(o2);
                else if(o1.isDirectory() && !o2.isDirectory()) return -1;
                else if(!o1.isDirectory() && o2.isDirectory()) return 1;
                else return o1.compareTo(o2);
            }
        }

        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.<br>
        * <b>User</b>: leizhimin<br>
        * <b>Date</b>: 2008-7-18 22:02:30<br>
        * <b>Note</b>: 模拟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 + "  <DIR>       ");

                    } 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();
            }
        }

[1] [2] 下一页

咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册