练习每种文件统计个数,按修改时间降序展示

一:统计一个文件夹中每种文件的个数并打印

如:

txt:3个

doc:1个

png:3个

二:将某个文件夹下的一级文件对象,按照最近修改时间降序展示,并显示修改时间(解析过)

如:

2.docx: 2022-07-04 16:30:52
1.txt: 2022-07-04 15:56:26
1.png: 2022-04-19 10:26:10

public class Test5 {
    public static int countTxt;
    public static int countDoc;
    public static int countPng;
    public static void main(String[] args) {
        File f = new File("D:/picture");
        String[] list = f.list();
        for (String s : list) {
            if (s.contains("txt")){
                countTxt++;
            }else if(s.contains("doc")){
                countDoc++;
            }else if (s.contains("png")){
                countPng++;
            }
        }
        System.out.println("txt:" + countTxt + "个");
        System.out.println("doc:" + countDoc + "个");
        System.out.println("png:" + countPng + "个");

        File f2 = new File("D:/picture/aaa/bbb");
        File[] list1 = f2.listFiles();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Long> map = new HashMap<>();
        for (File file : list1) {
            map.put(file.getName(), file.lastModified());
        }
        List<Map.Entry<String, Long>> tem = new ArrayList(map.entrySet());
        Collections.sort(tem, (o1, o2) -> o2.getValue().compareTo(o1.getValue()));
        for (Map.Entry<String, Long> string : tem) {
            System.out.println(string.getKey() + ": " + sdf.format(string.getValue()));
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未满3岁

我爱你,你爱我,蜜雪冰城甜蜜蜜

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值