您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天就跟大家聊聊有关使用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 { @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
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盘文件统计工具有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。