您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。