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 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(); } } }
源碼下載地址
https://gitee.com/lanseyouyu/sediaer-calcdir.git
到此這篇關於java代碼實現C盤文件統計工具的文章就介紹到這瞭,更多相關java c盤文件統計工具內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Java 並發編程之ForkJoin框架
- Java多線程高並發中的Fork/Join框架機制詳解
- Java並發編程之Fork/Join框架的理解
- 創建Java線程安全類的七種方法
- java System類和Arrays類詳解