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

(一)单线程递归方式

创新互联建站专注于企业营销型网站、网站重做改版、横山网站定制设计、自适应品牌网站建设、html5商城网站定制开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为横山等各大城市提供网站开发制作服务。

 
 
  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 subDirectories; 
  18.         public SubDirectoriesAndSize(final long totalSize, 
  19.                 final List 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 subDirectories = new ArrayList(); 
  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 directories = new ArrayList(); 
  45.             directories.add(file); 
  46.             while (!directories.isEmpty()) { 
  47.                 final List> partialResults = new ArrayList>(); 
  48.                 for (final File directory : directories) { 
  49.                     partialResults.add(service 
  50.                             .submit(new Callable() { 
  51.                                 public SubDirectoriesAndSize call() { 
  52.                                     return getTotalAndSubDirs(directory); 
  53.                                 } 
  54.                             })); 
  55.                 } 
  56.                 directories.clear(); 
  57.                 for (final Future 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> partialTotalFutures = new ArrayList>(); 
  24.             for (final File child : children) { 
  25.                 partialTotalFutures.add(service.submit(new Callable() { 
  26.                     public Long call() throws InterruptedException, 
  27.                             ExecutionException, TimeoutException { 
  28.                         return getTotalSizeOfFilesInDir(service, child); 
  29.                     } 
  30.                 })); 
  31.             } 
  32.             for (final Future 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.     } 

#p#

(四)使用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 fileSizes = new ArrayBlockingQueue
  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 { 
  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> tasks = new ArrayList>(); 
  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 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.     } 

网站题目:Java读取文件夹大小的6种方法及代码
文章网址:http://www.gawzjz.com/qtweb/news21/184171.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联