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

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

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.     } 

#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<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.     } 
责任编辑:王雪燕 来源: codeceo
相关推荐

2010-08-02 16:47:46

Flex

2024-01-31 09:24:58

2020-05-15 10:09:17

优化创新数字化转型CIO

2020-06-23 09:52:31

运营效率首席信息官IT预算

2023-03-28 15:19:37

文件列表scandir函数

2020-09-23 08:53:48

父文件夹模块Python

2019-10-08 10:28:36

Python程序员镜音双子

2009-08-12 16:57:28

C#读取文件夹

2018-12-19 14:43:13

Linux命令搜索文件

2024-08-29 08:04:14

2024-03-15 11:35:11

配置文件应用程序开发

2016-10-12 13:53:38

JavaByteBufferRandomAcces

2021-02-05 08:03:52

Java

2009-06-26 10:05:10

获取文件大小

2020-12-22 08:15:05

Java字节流字符流

2010-08-26 09:16:42

Web服务器

2024-08-19 01:00:00

读取配置文件接口应用程序

2013-10-21 14:26:04

2021-09-30 07:26:15

Linux文件夹windows

2017-11-03 10:40:25

Python复制文件方法
点赞
收藏

51CTO技术栈公众号