服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|R语言|

服务器之家 - 编程语言 - Java教程 - java代码实现C盘文件统计工具

java代码实现C盘文件统计工具

2021-10-06 01:12zhangxiaojun34901 Java教程

今天周末,给大家分享基于java代码实现C盘文件统计工具,在这小编使用的版本是Maven-3.9.9,jdk1.8,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

 概述

java代码实现C盘文件统计工具

工作电脑用了3年多了,100G的C盘也快吃不消了,每次打开看到C盘红了,总要用清理工具清理一下子.

不知道怎么最近清理工具清理了也没有什么效果,把temp下面的文件删除,没什么大作用.

总是有一种冲动要去处理掉,已经影响工作了.乘着星期天写个工具,分析一下C盘的文件,看看那些是最近才增加的,能不能手动删除掉.

版本介绍

  • Maven-3.3.9
  • JDK1.8 目录结构
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
com
  └─sedaier
      └─calcdir
          ├─service
          │  │  IDirSizeCalc.java
          │  │
          │  └─impl
          │         DirSizeCalcByForkJoinPoolImpl.java
          
          ├─test
          │      Test.java
          
          └─tool
                 OutputFileUtils.java //日志输出工具类

源码

DirSizeCalcByForkJoinPoolImpl.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.sedaier.calcdir.service.impl;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
 
import com.sedaier.calcdir.service.IDirSizeCalc;
import com.sedaier.calcdir.tool.OutputFileUtils;
 
public class DirSizeCalcByForkJoinPoolImpl implements IDirSizeCalc {
    
    @Override
    public long getSize(final File file, final File log) {
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        return forkJoinPool.invoke(new FileSizeFinder(file, log));
    }
 
    @SuppressWarnings("serial")
    private static class FileSizeFinder extends RecursiveTask<Long> {
        final File file;
        final File log;
 
        FileSizeFinder(final File theFile, final File theLog) {
            file = theFile;
            log = theLog;
        }
 
        @Override
        public Long compute() {
            long size = 0;
            if (file.isFile()) {
                size = file.length();
                OutputFileUtils.write(log, file);
            } else {
                final File[] children = file.listFiles();
                if (children != null) {
                    List<ForkJoinTask<Long>> tasks = new ArrayList<ForkJoinTask<Long>>();
                    for (final File child : children) {
                        if (child.isFile()) {
                            size += child.length();
                            OutputFileUtils.write(log, child);
                        } else {
                            tasks.add(new FileSizeFinder(child, log));
                        }
                    }
                    for (final ForkJoinTask<Long> task : invokeAll(tasks)) {
                        size += task.join();
                    }
                }
            }
            return size;
        }
    }
}

IDirSizeCalc.java

?
1
2
3
4
5
6
7
package com.sedaier.calcdir.service;
 
import java.io.File;
 
public interface IDirSizeCalc {
    long getSize(final File file, final File log);
}

Test.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.sedaier.calcdir.test;
 
import java.io.File;
 
import com.sedaier.calcdir.service.IDirSizeCalc;
import com.sedaier.calcdir.service.impl.DirSizeCalcByForkJoinPoolImpl;
import com.sedaier.calcdir.tool.OutputFileUtils;
 
public class Test {
    public static void main(final String[] args) {
        String dirPath = "C:\\Windows";
        String log = "C:\\Users\\Administrator\\Desktop\\log.log";
        File file = new File(dirPath);
        File logFile = new File(log);
 
        calcDirSize(file, logFile, new DirSizeCalcByForkJoinPoolImpl());
        OutputFileUtils.distory();
    }
 
    private static void calcDirSize(File file, File log, IDirSizeCalc dirSizeCalc) {
        dirSizeCalc.getSize(file, log);
    }
}

OutputFileUtils.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.sedaier.calcdir.tool;
 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
 
public class OutputFileUtils {
 
    public static int time = 0;
    //buffer刷信息时间
    public static int overtime = 100;
    //避免重复初始化
    public static boolean flag = false;
    public static FileWriter out = null;
    public static BufferedWriter bw = null;
    //昨天
    public static long day;
    //用于计算日期 -1 当前日期往前一天,-2 当前日期往前两天
    public static int amount = -1;
    
    static {
        //初始化日期
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_MONTH, amount);
        day = instance.getTimeInMillis();
    }
    
    public static void write(final File log, File file) {
        
        try {
            if (!flag && !log.exists()) {
                log.createNewFile();
                flag = true;
            }
            if(out == null || bw == null) {
                out = new FileWriter (log);
                bw = new BufferedWriter(out);
            }
            long lastModified = file.lastModified();
            if(lastModified > day) {
                bw.write(String.format("%s===大小=%d===时间=%tF",file.getAbsolutePath(),file.length()/1000000,lastModified));
                bw.newLine();
            }
            time++;
            if(time > overtime) {
                bw.flush();
                time = 0 ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void distory() {
        try {
            bw.close();
            out.close();
            System.out.println("关闭成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

源码下载地址

https://gitee.com/lanseyouyu/sediaer-calcdir.git

到此这篇关于java代码实现C盘文件统计工具的文章就介绍到这了,更多相关java c盘文件统计工具内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq125281823/article/details/118651166

延伸 · 阅读

精彩推荐