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

网友投稿 270 2022-12-28

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

概述

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

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

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

版本介绍

Maven-3.3.9

JDK1.8 目录结构

com

└─sedaier

└─calcdir

├─service

│ │ IDirSizeCalc.java

│ │

│ └─impl

│ DirSizeCalcByForkJoinPoolImpl.java

├─test

│ Test.java

└─tool

OutputFileUtils.java //日志输出工具类

源码

DirSizeCalcByForkJoinPoolImpl.java

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 DirSizeCalcByForkJoinPhttp://oolImpl 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 {

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> tasks = new ArrayList>();

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 task : invokeAll(tasks)) {

size += task.join();

}

}

}

return size;

}

}

}

IDirSizeCalc.java

package com.sedaier.calcdir.service;

import java.io.File;

public interface IDirSizeCalc {

long getSize(final File file, final File log);

}

Test.java

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

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(kPRPRVOout);

}

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java异常级别与捕获的示例代码
下一篇:java控制台实现聊天程序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~