今天就跟大家聊聊有关使用java怎么实现一个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 DirSizeCalcByForkJoinPoolImpl implements IDirSizeCalc {@Overridepublic 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;}@Overridepublic 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
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(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();}}}
看完上述内容,你们对使用java怎么实现一个C盘文件统计工具有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。