listfile.java

来自「一款少见的用swt写的彩票软件」· Java 代码 · 共 53 行

JAVA
53
字号
package com.dc.test;

import java.io.File;

public class ListFile {

    private long[] count = new long[] { 0, 0 };
    
    private File file;

    private long[] listFile(String path) {
        file = new File(path);
        File[] f = file.listFiles();
        for (int i = 0; i < f.length; i++) {
            if (f[i].isDirectory()) {
                count[0]++;
                this.listFile(f[i].getPath());
                System.out.println("路径:"+f[i].getPath());
            } else {
                count[1]++;
            }
        }
        return count;
    }

    /**
     * 得到指定路径下的文件和文件夹数量
     * 
     * @param path
     *            要查看的路径
     * @return object[0]耗时(毫秒)<br>
     *         object[1]文件夹数量<br>
     *         object[2]文件数量
     */
    public Object[] getFileCount(String path) {
        long t = System.currentTimeMillis();
        long[] count = this.listFile(path);
        t = System.currentTimeMillis() - t;
        Object[] o = new Object[] { Long.valueOf(t), Long.valueOf(count[0]),
                Long.valueOf(count[1])};
        return o;
    }

    public static void main(String[] args) {
        ListFile l = new ListFile();
        Object[] count = l.getFileCount("d:\\temp\\");
        System.out.println("查找耗时(毫秒)"+count[0]);
        System.out.println("文件夹数量为:"+count[1]);
        System.out.println("文件数量为:"+count[2]);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?