您现在的位置是:首页 > 文章详情

从目录中查找最大和最小的文件(不包括子目录)

日期:2019-03-13点击:420

从目录中查找最大和最小的文件(不包括子目录),学会使用File类的一些基本方法的调用。代码如下:

package file;

import java.io.File;

public class FindMinAndMaxFile {
    public static void main(String[] args) {
        File windows = new File("c:/windows");
        File[] fs = windows.listFiles();
        int min = -1;
        int max = -1;

        int j = min + 1;
        while (j < fs.length) {
            if (fs[j].isFile() && fs[j].length() > 0 && min < 0) {
                min = max = j;
                j++;
                continue;
            }
            if (fs[j].isFile() && (fs[j].length() != 0)) {
                if (fs[j].length() < fs[min].length())
                    min = j;
                if (fs[j].length() > fs[max].length())
                    max = j;
            }
            j++;
        }

        System.out.println("the min file is:" + fs[min].getName() + " and it's length is " + fs[min].length());
        System.out.println("the max file is:" + fs[max].getName() + " and it's length is " + fs[max].length());
    }
}
原文链接:https://yq.aliyun.com/articles/693612
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章