总是发现自己的C盘快满了,清理程序也不会帮我自己清理,然后就自己写了一个程序去读取磁盘下的目录占多大空间,目前只能读取总的大小,正在整改看能不能把每个目录总的大小打印出来,这样就可以去删除了,否则自己挨个的去看,岂不是很傻,毕竟自己也是个屌丝程序员啊。
下面的都是从网上摘录下的,我拿的这几个都能跑,不能跑的,我就不贴出来了。
方法一:
package com.beijishiqidu.file; import java.io.File; public class _01_TotalFileSizeSequential { public static void main(String[] args) { final long start = System.nanoTime(); String filePath = "C:\\Users\\Administrator"; File file = new File(filePath); Long result = getTotalFileSize(file); final long end = System.nanoTime(); System.out.println("总大小为(G):" + result / 1024d / 1024d / 1024d); System.out.println("_01_TotalFileSizeSequential ----> Time taken(秒): " + (end - start) / 1.0e9); } private static long getTotalFileSize(File file) { if (file.isFile()) { return file.length(); } File[] fileArray = file.listFiles(); long total = 0; if (fileArray != null) { for (File tmpFile : fileArray) { total += getTotalFileSize(tmpFile); } } return total; } }
运行结果:
总大小为(G):8.57133452873677 _01_TotalFileSizeSequential ----> Time taken(秒): 11.744519825
方法二:
package com.beijishiqidu.file; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; public class _02_ConcurrentTotalFileSize { public static void main(String[] args) { final long start = System.nanoTime(); String filePath = "C:\\Users\\Administrator"; File file = new File(filePath); Long result = new _02_ConcurrentTotalFileSize().getTotalSizeOfFilesInDir(file); final long end = System.nanoTime(); System.out.println("总大小为(G):" + result / 1024d / 1024d / 1024d); System.out.println("_02_ConcurrentTotalFileSize ----> Time taken(秒): " + (end - start) / 1.0e9); } class SubDirectoriesAndSize { final public long size; final public ListsubDirectories; public SubDirectoriesAndSize(final long totalSize, final List theSubDirs) { size = totalSize; subDirectories = Collections.unmodifiableList(theSubDirs); } } private SubDirectoriesAndSize getTotalAndSubDirs(final File file) { long total = 0; final List subDirectories = new ArrayList (); if (file.isDirectory()) { final File[] children = file.listFiles(); if (children != null) { for (final File child : children) { if (child.isFile()) { total += child.length(); } else { subDirectories.add(child); } } } } return new SubDirectoriesAndSize(total, subDirectories); } private long getTotalSizeOfFilesInDir(final File file) { final ExecutorService service = Executors.newFixedThreadPool(100); long total = 0; try { final List directories = new ArrayList (); directories.add(file); while (!directories.isEmpty()) { final List > partialResults = new ArrayList >(); for (final File directory : directories) { partialResults.add(service.submit(new Callable () { public SubDirectoriesAndSize call() { return getTotalAndSubDirs(directory); } })); } directories.clear(); for (final Future partialResultFuture : partialResults) { final SubDirectoriesAndSize subDirectoriesAndSize = partialResultFuture.get(100, TimeUnit.SECONDS); directories.addAll(subDirectoriesAndSize.subDirectories); total += subDirectoriesAndSize.size; } } } catch (Exception e) { e.printStackTrace(); } finally { service.shutdown(); } return total; } }
运行结果:
总大小为(G):8.571664821356535 _02_ConcurrentTotalFileSize ----> Time taken(秒): 7.665382601
方法三:
package com.beijishiqidu.file; import java.io.File; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; public class _04_ConcurrentTotalFileSizeWLatch { private ExecutorService service; final private AtomicLong pendingFileVisits = new AtomicLong(); final private AtomicLong totalSize = new AtomicLong(); final private CountDownLatch latch = new CountDownLatch(1); public static void main(String[] args) throws Exception { final long start = System.nanoTime(); String filePath = "C:\\Users\\Administrator"; File file = new File(filePath); Long result = new _04_ConcurrentTotalFileSizeWLatch().getTotalSizeOfFile(file); final long end = System.nanoTime(); System.out.println("总大小为(G):" + result / 1024d / 1024d / 1024d); System.out.println("_04_ConcurrentTotalFileSizeWLatch ----> Time taken(秒): " + (end - start) / 1.0e9); } private void updateTotalSizeOfFilesInDir(final File file) { long fileSize = 0; if (file.isFile()) { fileSize = file.length(); } else { final File[] children = file.listFiles(); if (children != null) { for (final File child : children) { if (child.isFile()) { fileSize += child.length(); } else { pendingFileVisits.incrementAndGet(); service.execute(new Runnable() { public void run() { updateTotalSizeOfFilesInDir(child); } }); } } } } totalSize.addAndGet(fileSize); if (pendingFileVisits.decrementAndGet() == 0) { latch.countDown(); } } private long getTotalSizeOfFile(final File file) throws InterruptedException { service = Executors.newFixedThreadPool(100); pendingFileVisits.incrementAndGet(); try { updateTotalSizeOfFilesInDir(file); latch.await(100, TimeUnit.SECONDS); return totalSize.longValue(); } finally { service.shutdown(); } } }
运行结果:
总大小为(G):8.572077584452927 _04_ConcurrentTotalFileSizeWLatch ----> Time taken(秒): 7.384791101
方法四:
package com.beijishiqidu.file; import java.io.File; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; public class _05_ConcurrentTotalFileSizeWQueue { private ExecutorService service; final private BlockingQueuefileSizes = new ArrayBlockingQueue (500); final AtomicLong pendingFileVisits = new AtomicLong(); public static void main(String[] args) throws Exception { final long start = System.nanoTime(); String filePath = "C:\\Users\\Administrator"; File file = new File(filePath); Long result = new _05_ConcurrentTotalFileSizeWQueue().getTotalSizeOfFile(file); final long end = System.nanoTime(); System.out.println("总大小为(G):" + result / 1024d / 1024d / 1024d); System.out.println("_05_ConcurrentTotalFileSizeWQueue ----> Time taken(秒): " + (end - start) / 1.0e9); } private void startExploreDir(final File file) { pendingFileVisits.incrementAndGet(); service.execute(new Runnable() { public void run() { exploreDir(file); } }); } private void exploreDir(final File file) { long fileSize = 0; if (file.isFile()) { fileSize = file.length(); } else { final File[] children = file.listFiles(); if (children != null) { for (final File child : children) { if (child.isFile()) { fileSize += child.length(); } else { startExploreDir(child); } } } } try { fileSizes.put(fileSize); } catch (Exception ex) { throw new RuntimeException(ex); } pendingFileVisits.decrementAndGet(); } private long getTotalSizeOfFile(final File file) throws InterruptedException { service = Executors.newFixedThreadPool(100); try { startExploreDir(file); long totalSize = 0; while (pendingFileVisits.get() > 0 || fileSizes.size() > 0) { final Long size = fileSizes.poll(10, TimeUnit.SECONDS); totalSize += size; } return totalSize; } finally { service.shutdown(); } } }
运行结果:
总大小为(G):8.572272757068276 _05_ConcurrentTotalFileSizeWQueue ----> Time taken(秒): 7.623957758
方法五:
package com.beijishiqidu.file; 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; public class _06_FileSizeWForkJoinPool { public static void main(String[] args) { final long start = System.nanoTime(); String filePath = "C:\\Users\\Administrator"; File file = new File(filePath); ForkJoinPool forkJoinPool = new ForkJoinPool(); Long result = forkJoinPool.invoke(new FileSizeFinder(file)); final long end = System.nanoTime(); System.out.println("总大小为(G):" + result / 1024d / 1024d / 1024d); System.out.println("_06_FileSizeWForkJoinPool ----> Time taken(秒): " + (end - start) / 1.0e9); } private static class FileSizeFinder extends RecursiveTask{ private static final long serialVersionUID = 8030791942789701702L; final File file; public FileSizeFinder(final File theFile) { file = theFile; } @Override public Long compute() { long size = 0; if (file.isFile()) { size = file.length(); } else { final File[] children = file.listFiles(); if (children != null) { List > tasks = new ArrayList >(); for (final File child : children) { if (child.isFile()) { size += child.length(); } else { tasks.add(new FileSizeFinder(child)); } } for (final ForkJoinTask task : invokeAll(tasks)) { size += task.join(); } } } return size; } } }
运行结果:
总大小为(G):8.572774960659444 _06_FileSizeWForkJoinPool ----> Time taken(秒): 5.658299909