中国领先的IT技术网站
|
|

Java读取文件夹大小的6种方法及代码

这篇文章介绍了JAVA读取文件夹大小的几种方法实例,有需要的朋友可以参考一下。

作者:来源:codeceo|2015-03-13 13:50


Java读取文件夹大小的6种方法及代码

(一)单线程递归方式

  1. package com.taobao.test; 
  2. import java.io.File; 
  3. public class TotalFileSizeSequential { 
  4.     public static String fileName = "C:\\Documents and Settings\\Administrator\\桌面\\monkeytalk"
  5.     // 递归方式 计算文件的大小 
  6.     private long getTotalSizeOfFilesInDir(final File file) { 
  7.         if (file.isFile()) 
  8.             return file.length(); 
  9.         final File[] children = file.listFiles(); 
  10.         long total = 0
  11.         if (children != null
  12.             for (final File child : children) 
  13.                 total += getTotalSizeOfFilesInDir(child); 
  14.         return total; 
  15.     } 
  16.     public static void main(final String[] args) { 
  17.         final long start = System.nanoTime(); 
  18.         final long total = new TotalFileSizeSequential() 
  19.                 .getTotalSizeOfFilesInDir(new File(fileName)); 
  20.         final long end = System.nanoTime(); 
  21.         System.out.println("Total Size: " + total); 
  22.         System.out.println("Time taken: " + (end - start) / 1.0e9); 
  23.     } 

(二)使用Executors.newFixedThreadPool和callable 多线程实现

  1. package com.taobao.test; 
  2. import java.io.File; 
  3. import java.util.ArrayList; 
  4. import java.util.Collections; 
  5. import java.util.List; 
  6. import java.util.concurrent.Callable; 
  7. import java.util.concurrent.ExecutionException; 
  8. import java.util.concurrent.ExecutorService; 
  9. import java.util.concurrent.Executors; 
  10. import java.util.concurrent.Future; 
  11. import java.util.concurrent.TimeUnit; 
  12. import java.util.concurrent.TimeoutException; 
  13. public class ConcurrentTotalFileSize { 
  14.     public static final String fileName = "C:\\Documents and Settings\\Administrator\\桌面\\monkeytalk"
  15.     class SubDirectoriesAndSize { 
  16.         final public long size; 
  17.         final public List<File> subDirectories; 
  18.         public SubDirectoriesAndSize(final long totalSize, 
  19.                 final List<File> theSubDirs) { 
  20.             size = totalSize; 
  21.             subDirectories = Collections.unmodifiableList(theSubDirs); 
  22.         } 
  23.     } 
  24.     private SubDirectoriesAndSize getTotalAndSubDirs(final File file) { 
  25.         long total = 0
  26.         final List<File> subDirectories = new ArrayList<File>(); 
  27.         if (file.isDirectory()) { 
  28.             final File[] children = file.listFiles(); 
  29.             if (children != null
  30.                 for (final File child : children) { 
  31.                     if (child.isFile()) 
  32.                         total += child.length(); 
  33.                     else 
  34.                         subDirectories.add(child); 
  35.                 } 
  36.         } 
  37.         return new SubDirectoriesAndSize(total, subDirectories); 
  38.     } 
  39.     private long getTotalSizeOfFilesInDir(final File file) 
  40.             throws InterruptedException, ExecutionException, TimeoutException { 
  41.         final ExecutorService service = Executors.newFixedThreadPool(100); 
  42.         try { 
  43.             long total = 0
  44.             final List<File> directories = new ArrayList<File>(); 
  45.             directories.add(file); 
  46.             while (!directories.isEmpty()) { 
  47.                 final List<Future<SubDirectoriesAndSize>> partialResults = new ArrayList<Future<SubDirectoriesAndSize>>(); 
  48.                 for (final File directory : directories) { 
  49.                     partialResults.add(service 
  50.                             .submit(new Callable<SubDirectoriesAndSize>() { 
  51.                                 public SubDirectoriesAndSize call() { 
  52.                                     return getTotalAndSubDirs(directory); 
  53.                                 } 
  54.                             })); 
  55.                 } 
  56.                 directories.clear(); 
  57.                 for (final Future<SubDirectoriesAndSize> partialResultFuture : partialResults) { 
  58.                     final SubDirectoriesAndSize subDirectoriesAndSize = partialResultFuture 
  59.                             .get(100, TimeUnit.SECONDS); 
  60.                     directories.addAll(subDirectoriesAndSize.subDirectories); 
  61.                     total += subDirectoriesAndSize.size; 
  62.                 } 
  63.             } 
  64.             return total; 
  65.         } finally { 
  66.             service.shutdown(); 
  67.         } 
  68.     } 
  69.     public static void main(final String[] args) throws InterruptedException, 
  70.             ExecutionException, TimeoutException { 
  71.         final long start = System.nanoTime(); 
  72.         final long total = new ConcurrentTotalFileSize() 
  73.                 .getTotalSizeOfFilesInDir(new File(fileName)); 
  74.         final long end = System.nanoTime(); 
  75.         System.out.println("Total Size: " + total); 
  76.         System.out.println("Time taken: " + (end - start) / 1.0e9); 
  77.     } 

(三)使用Executors.newFixedThreadPool和callable 多线程的另外一种实现

  1. package com.taobao.test; 
  2.  
  3. import java.io.File; 
  4. import java.util.ArrayList; 
  5. import java.util.List; 
  6. import java.util.concurrent.Callable; 
  7. import java.util.concurrent.ExecutionException; 
  8. import java.util.concurrent.ExecutorService; 
  9. import java.util.concurrent.Executors; 
  10. import java.util.concurrent.Future; 
  11. import java.util.concurrent.TimeUnit; 
  12. import java.util.concurrent.TimeoutException; 
  13. public class NaivelyConcurrentTotalFileSize { 
  14.     public static String fileName = "C:\\Documents and Settings\\Administrator\\桌面\\monkeytalk"
  15.     private long getTotalSizeOfFilesInDir(final ExecutorService service, 
  16.             final File file) throws InterruptedException, ExecutionException, 
  17.             TimeoutException { 
  18.         if (file.isFile()) 
  19.             return file.length(); 
  20.         long total = 0
  21.         final File[] children = file.listFiles(); 
  22.         if (children != null) { 
  23.             final List<Future<Long>> partialTotalFutures = new ArrayList<Future<Long>>(); 
  24.             for (final File child : children) { 
  25.                 partialTotalFutures.add(service.submit(new Callable<Long>() { 
  26.                     public Long call() throws InterruptedException, 
  27.                             ExecutionException, TimeoutException { 
  28.                         return getTotalSizeOfFilesInDir(service, child); 
  29.                     } 
  30.                 })); 
  31.             } 
  32.             for (final Future<Long> partialTotalFuture : partialTotalFutures) 
  33.                 total += partialTotalFuture.get(100, TimeUnit.SECONDS); 
  34.         } 
  35.         return total; 
  36.     } 
  37.     private long getTotalSizeOfFile(final String fileName) 
  38.             throws InterruptedException, ExecutionException, TimeoutException { 
  39.         final ExecutorService service = Executors.newFixedThreadPool(100); 
  40.         try { 
  41.             return getTotalSizeOfFilesInDir(service, new File(fileName)); 
  42.         } finally { 
  43.             service.shutdown(); 
  44.         } 
  45.     } 
  46.     public static void main(final String[] args) throws InterruptedException, 
  47.             ExecutionException, TimeoutException { 
  48.         final long start = System.nanoTime(); 
  49.         final long total = new NaivelyConcurrentTotalFileSize() 
  50.                 .getTotalSizeOfFile(fileName); 
  51.         final long end = System.nanoTime(); 
  52.         System.out.println("Total Size: " + total); 
  53.         System.out.println("Time taken: " + (end - start) / 1.0e9); 
  54.     } 

(四)使用CountDownLatch和AtomicLong实现多线程下的并发控制

  1. package com.taobao.test; 
  2. import java.io.File; 
  3. import java.util.concurrent.CountDownLatch; 
  4. import java.util.concurrent.ExecutorService; 
  5. import java.util.concurrent.Executors; 
  6. import java.util.concurrent.TimeUnit; 
  7. import java.util.concurrent.atomic.AtomicLong; 
  8. public class ConcurrentTotalFileSizeWLatch { 
  9.     private ExecutorService service; 
  10.     final private AtomicLong pendingFileVisits = new AtomicLong(); 
  11.     final private AtomicLong totalSize = new AtomicLong(); 
  12.     final private CountDownLatch latch = new CountDownLatch(1); 
  13.     public static String fileName = "C:\\Documents and Settings\\Administrator\\桌面\\monkeytalk"
  14.     private void updateTotalSizeOfFilesInDir(final File file) { 
  15.         long fileSize = 0
  16.         if (file.isFile()) 
  17.             fileSize = file.length(); 
  18.         else { 
  19.             final File[] children = file.listFiles(); 
  20.             if (children != null) { 
  21.                 for (final File child : children) { 
  22.                     if (child.isFile()) 
  23.                         fileSize += child.length(); 
  24.                     else { 
  25.                         pendingFileVisits.incrementAndGet(); 
  26.                         service.execute(new Runnable() { 
  27.                             public void run() { 
  28.                                 updateTotalSizeOfFilesInDir(child); 
  29.                             } 
  30.                         }); 
  31.                     } 
  32.                 } 
  33.             } 
  34.         } 
  35.         totalSize.addAndGet(fileSize); 
  36.         if (pendingFileVisits.decrementAndGet() == 0
  37.             latch.countDown(); 
  38.     } 
  39.     private long getTotalSizeOfFile(final String fileName) 
  40.             throws InterruptedException { 
  41.         service = Executors.newFixedThreadPool(100); 
  42.         pendingFileVisits.incrementAndGet(); 
  43.         try { 
  44.             updateTotalSizeOfFilesInDir(new File(fileName)); 
  45.             latch.await(100, TimeUnit.SECONDS); 
  46.             return totalSize.longValue(); 
  47.         } finally { 
  48.             service.shutdown(); 
  49.         } 
  50.     } 
  51.     public static void main(final String[] args) throws InterruptedException { 
  52.         final long start = System.nanoTime(); 
  53.         final long total = new ConcurrentTotalFileSizeWLatch() 
  54.                 .getTotalSizeOfFile(fileName); 
  55.         final long end = System.nanoTime(); 
  56.         System.out.println("Total Size: " + total); 
  57.         System.out.println("Time taken: " + (end - start) / 1.0e9); 
  58.     } 

(五)使用BlockingQueue和AtomicLong的实现

  1. package com.taobao.test; 
  2. import java.io.File; 
  3. import java.util.concurrent.ArrayBlockingQueue; 
  4. import java.util.concurrent.BlockingQueue; 
  5. import java.util.concurrent.ExecutorService; 
  6. import java.util.concurrent.Executors; 
  7. import java.util.concurrent.TimeUnit; 
  8. import java.util.concurrent.atomic.AtomicLong; 
  9. public class ConcurrentTotalFileSizeWQueue { 
  10.     public static String fileName = "C:\\Documents and Settings\\Administrator\\桌面\\monkeytalk"
  11.  
  12.     private ExecutorService service; 
  13.     final private BlockingQueue<Long> fileSizes = new ArrayBlockingQueue<Long>( 
  14.             500); 
  15.     final AtomicLong pendingFileVisits = new AtomicLong(); 
  16.     private void startExploreDir(final File file) { 
  17.         pendingFileVisits.incrementAndGet(); 
  18.         service.execute(new Runnable() { 
  19.             public void run() { 
  20.                 exploreDir(file); 
  21.             } 
  22.         }); 
  23.     } 
  24.     private void exploreDir(final File file) { 
  25.         long fileSize = 0
  26.         if (file.isFile()) 
  27.             fileSize = file.length(); 
  28.         else { 
  29.             final File[] children = file.listFiles(); 
  30.             if (children != null
  31.                 for (final File child : children) { 
  32.                     if (child.isFile()) 
  33.                         fileSize += child.length(); 
  34.                     else { 
  35.                         startExploreDir(child); 
  36.                     } 
  37.                 } 
  38.         } 
  39.         try { 
  40.             fileSizes.put(fileSize); 
  41.         } catch (Exception ex) { 
  42.             throw new RuntimeException(ex); 
  43.         } 
  44.         pendingFileVisits.decrementAndGet(); 
  45.     } 
  46.     private long getTotalSizeOfFile(final String fileName) 
  47.             throws InterruptedException { 
  48.         service = Executors.newFixedThreadPool(100); 
  49.         try { 
  50.             startExploreDir(new File(fileName)); 
  51.             long totalSize = 0
  52.             while (pendingFileVisits.get() > 0 || fileSizes.size() > 0) { 
  53.                 final Long size = fileSizes.poll(10, TimeUnit.SECONDS); 
  54.                 totalSize += size; 
  55.             } 
  56.             return totalSize; 
  57.         } finally { 
  58.             service.shutdown(); 
  59.         } 
  60.     } 
  61.     public static void main(final String[] args) throws InterruptedException { 
  62.         final long start = System.nanoTime(); 
  63.         final long total = new ConcurrentTotalFileSizeWQueue() 
  64.                 .getTotalSizeOfFile(fileName); 
  65.         final long end = System.nanoTime(); 
  66.         System.out.println("Total Size: " + total); 
  67.         System.out.println("Time taken: " + (end - start) / 1.0e9); 
  68.     } 

(六)使用jdk7的ForkJoin来实现

  1. package com.taobao.test; 
  2. import java.io.File; 
  3. import java.util.ArrayList; 
  4. import java.util.List; 
  5. import java.util.concurrent.ForkJoinPool; 
  6. import java.util.concurrent.ForkJoinTask; 
  7. import java.util.concurrent.RecursiveTask; 
  8. public class FileSize { 
  9.     private final static ForkJoinPool forkJoinPool = new ForkJoinPool(); 
  10.     public static String fileName = "C:\\Documents and Settings\\Administrator\\桌面\\monkeytalk"
  11.  
  12.     private static class FileSizeFinder extends RecursiveTask<Long> { 
  13.         final File file; 
  14.         public FileSizeFinder(final File theFile) { 
  15.             file = theFile; 
  16.         } 
  17.         @Override 
  18.         public Long compute() { 
  19.             long size = 0
  20.             if (file.isFile()) { 
  21.                 size = file.length(); 
  22.             } else { 
  23.                 final File[] children = file.listFiles(); 
  24.                 if (children != null) { 
  25.                     List<ForkJoinTask<Long>> tasks = new ArrayList<ForkJoinTask<Long>>(); 
  26.                     for (final File child : children) { 
  27.                         if (child.isFile()) { 
  28.                             size += child.length(); 
  29.                         } else { 
  30.                             tasks.add(new FileSizeFinder(child)); 
  31.                         } 
  32.                     } 
  33.                     for (final ForkJoinTask<Long> task : invokeAll(tasks)) { 
  34.                         size += task.join(); 
  35.                     } 
  36.                 } 
  37.             } 
  38.             return size; 
  39.         } 
  40.     } 
  41.     public static void main(final String[] args) { 
  42.         final long start = System.nanoTime(); 
  43.         final long total = forkJoinPool.invoke(new FileSizeFinder(new File("/home"))); 
  44.         final long end = System.nanoTime(); 
  45.         System.out.println("Total Size: " + total); 
  46.         System.out.println("Time taken: " + (end - start) / 1.0e9); 
  47.     } 

【编辑推荐】

  1. 关于Java读取xml文件的学习
  2. 浅谈Java读取Csv实践
  3. 浅谈Java读取Csv实践
  4. Java读取WEB应用中的资源
【责任编辑:wangxueyan TEL:(010)68476606】

点赞 0
分享:
大家都在看
猜你喜欢

读 书 +更多

PHP和MySQL Web开发(原书第3版)

本书将介绍如何创建可交互的Web站点,包括从最简单的订单表单到复杂的安全电子商务站点。而且,读者还将了解如何使用开放源代码技术来实现...

订阅51CTO邮刊

点击这里查看样刊

订阅51CTO邮刊