Java 获取文件的创建时间以及文件夹下面所有文件

18 篇文章 0 订阅
13 篇文章 0 订阅

        为啥这篇文章,是因为有需求,所以写了一下。是为了写了一个分享,文件有效期,到期了然后就移除该文件。话不多说上代码

获取文件后缀为.aup3 \ .opus \  .pcm  看你们自己的需求

/**
     * 获取文件的创建时间
     *
     * @param filePath 文件路径
     * @return
     */
    public static String getCreateTime(String filePath) {
        try {
            String strTime = null;
            Process p = Runtime.getRuntime().exec("cmd /C dir " + filePath + "/tc");
            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = br.readLine()) != null) {
                if (line.endsWith(".aup3") || line.endsWith(".opus") || line.endsWith(".pcm")) {
                    strTime = line.substring(0, 17);
                    break;
                }
            }
            return strTime;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

获取文件夹下的所有文件

 /**
     * 获取文件夹下所有文件
     * @param filePath
     */
    public static List<String> getFileName(String filePath) {
        if (StringUtils.isNotBlank(filePath)) {
            File file = new File(filePath);
            //判断文件或目录是否存在
            if (!file.exists()) {
                System.out.println("【" + filePath + " not exists】");
                return null;
            }
            //获取该文件夹下所有的文件
            File[] fileArray = file.listFiles();
            File fileName = null;
            List<String> arrayList = new ArrayList<>();
            for (int i = 0; i < Objects.requireNonNull(fileArray).length; i++) {
                fileName = fileArray[i];
                //判断此文件是否存在
                if (fileName.isDirectory()) {
                    System.out.println("【目录:" + fileName.getName() + "】");
                    return null;
                } else {
                    System.out.println(fileName.getName());
                    arrayList.add(fileName.getName());
                }
            }
            return arrayList;
        }
        return null;
    }

获取文件的创建时间的时间戳

/**
     * 返回文件创建时间的时间戳
     * @param filePath
     * @return
     */
    public static Long createTimeFile(String filePath) {
        String createTime = getCreateTime(filePath);
        String s = createTime + ":00";
        System.out.println("获得文件时间:" + s);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd  HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(s, df).plusDays(3);
        return ldt.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

根据文件绝对路径移除文件

   /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static boolean delete(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            return file.delete();
        }
        return false;
    }

测试mian方法

  public static void main(String[] args) throws ParseException {
//        String path = "D:\\resource\\file\\shareData\\1632887450482.opus";
//        deleteFile(path);
        String path = "D:\\resource\\file\\shareData\\";
        List<String> fileName = getFileName(path);
        if (CollectionUtil.isNotEmpty(fileName)  || fileName.size() >0 ){
            for (String s : fileName) {
                //1.获取文件时间戳
                Long createTime = createTimeFile(path + s);
                // 2、获取当前得时间戳
                long nowTime = System.currentTimeMillis();
                if(createTime < nowTime){
                    delete(path+s);
                }
            }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值