当前位置:  编程技术>java/j2ee

JAVA读取文件夹大小的几种方法实例

    来源: 互联网  发布时间:2014-10-28

    本文导语:  (一)单线程递归方式 代码如下:package com.taobao.test; import java.io.File; public class TotalFileSizeSequential {     public static String fileName = "C:\Documents and Settings\Administrator\桌面\monkeytalk";     // 递归方式 计算文件的大小    private long getTota...

(一)单线程递归方式

代码如下:

package com.taobao.test;

import java.io.File;

public class TotalFileSizeSequential {

    public static String fileName = "C:\Documents and Settings\Administrator\桌面\monkeytalk";

    // 递归方式 计算文件的大小
    private long getTotalSizeOfFilesInDir(final File file) {
        if (file.isFile())
            return file.length();
        final File[] children = file.listFiles();
        long total = 0;
        if (children != null)
            for (final File child : children)
                total += getTotalSizeOfFilesInDir(child);
        return total;
    }

    public static void main(final String[] args) {
        final long start = System.nanoTime();

        final long total = new TotalFileSizeSequential()
                .getTotalSizeOfFilesInDir(new File(fileName));
        final long end = System.nanoTime();
        System.out.println("Total Size: " + total);
        System.out.println("Time taken: " + (end - start) / 1.0e9);
    }
}

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

代码如下:

package com.taobao.test;

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.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ConcurrentTotalFileSize {

    public static final String fileName = "C:\Documents and Settings\Administrator\桌面\monkeytalk";

    class SubDirectoriesAndSize {

        final public long size;
        final public List subDirectories;

        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)
            throws InterruptedException, ExecutionException, TimeoutException {
        final ExecutorService service = Executors.newFixedThreadPool(100);
        try {
            long total = 0;
            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;
                }
            }
            return total;
        } finally {
            service.shutdown();
        }
    }

    public static void main(final String[] args) throws InterruptedException,
            ExecutionException, TimeoutException {
        final long start = System.nanoTime();
        final long total = new ConcurrentTotalFileSize()
                .getTotalSizeOfFilesInDir(new File(fileName));
        final long end = System.nanoTime();
        System.out.println("Total Size: " + total);
        System.out.println("Time taken: " + (end - start) / 1.0e9);
    }
}

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

代码如下:

package com.taobao.test;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class NaivelyConcurrentTotalFileSize {
    public static String fileName = "C:\Documents and Settings\Administrator\桌面\monkeytalk";

    private long getTotalSizeOfFilesInDir(final ExecutorService service,
            final File file) throws InterruptedException, ExecutionException,
            TimeoutException {
        if (file.isFile())
            return file.length();

        long total = 0;
        final File[] children = file.listFiles();

        if (children != null) {
            final List partialTotalFutures = new ArrayList();
            for (final File child : children) {
                partialTotalFutures.add(service.submit(new Callable() {
                    public Long call() throws InterruptedException,
                            ExecutionException, TimeoutException {
                        return getTotalSizeOfFilesInDir(service, child);
                    }
                }));
            }

            for (final Future partialTotalFuture : partialTotalFutures)
                total += partialTotalFuture.get(100, TimeUnit.SECONDS);

        }

        return total;

    }

    private long getTotalSizeOfFile(final String fileName)
            throws InterruptedException, ExecutionException, TimeoutException {
        final ExecutorService service = Executors.newFixedThreadPool(100);
        try {
            return getTotalSizeOfFilesInDir(service, new File(fileName));
        } finally {
            service.shutdown();
        }
    }

    public static void main(final String[] args) throws InterruptedException,
            ExecutionException, TimeoutException {
        final long start = System.nanoTime();
        final long total = new NaivelyConcurrentTotalFileSize()
                .getTotalSizeOfFile(fileName);
        final long end = System.nanoTime();
        System.out.println("Total Size: " + total);
        System.out.println("Time taken: " + (end - start) / 1.0e9);
    }
}

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

代码如下:

package com.taobao.test;

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 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 String fileName = "C:\Documents and Settings\Administrator\桌面\monkeytalk";

    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 String fileName)
            throws InterruptedException {
        service = Executors.newFixedThreadPool(100);
        pendingFileVisits.incrementAndGet();
        try {
            updateTotalSizeOfFilesInDir(new File(fileName));
            latch.await(100, TimeUnit.SECONDS);
            return totalSize.longValue();
        } finally {
            service.shutdown();
        }
    }

    public static void main(final String[] args) throws InterruptedException {
        final long start = System.nanoTime();
        final long total = new ConcurrentTotalFileSizeWLatch()
                .getTotalSizeOfFile(fileName);
        final long end = System.nanoTime();
        System.out.println("Total Size: " + total);
        System.out.println("Time taken: " + (end - start) / 1.0e9);
    }
}

(五)使用BlockingQueue和AtomicLong的实现

代码如下:

package com.taobao.test;

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 ConcurrentTotalFileSizeWQueue {
    public static String fileName = "C:\Documents and Settings\Administrator\桌面\monkeytalk";


    private ExecutorService service;
    final private BlockingQueue fileSizes = new ArrayBlockingQueue(
            500);
    final AtomicLong pendingFileVisits = new AtomicLong();

    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 String fileName)
            throws InterruptedException {
        service = Executors.newFixedThreadPool(100);
        try {
            startExploreDir(new File(fileName));
            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();
        }
    }

    public static void main(final String[] args) throws InterruptedException {
        final long start = System.nanoTime();
        final long total = new ConcurrentTotalFileSizeWQueue()
                .getTotalSizeOfFile(fileName);
        final long end = System.nanoTime();
        System.out.println("Total Size: " + total);
        System.out.println("Time taken: " + (end - start) / 1.0e9);
    }
}

(六)使用jdk7的ForkJoin来实现

代码如下:

package com.taobao.test;

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 FileSize {
    private final static ForkJoinPool forkJoinPool = new ForkJoinPool();
    public static String fileName = "C:\Documents and Settings\Administrator\桌面\monkeytalk";


    private static class FileSizeFinder extends RecursiveTask {
        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;
        }
    }

    public static void main(final String[] args) {
        final long start = System.nanoTime();
        final long total = forkJoinPool.invoke(new FileSizeFinder(new File("/home")));
        final long end = System.nanoTime();
        System.out.println("Total Size: " + total);
        System.out.println("Time taken: " + (end - start) / 1.0e9);
    }
}


    
 
 

您可能感兴趣的文章:

  • 用jdk编译Java能不能让编译生成的.class文件自动保存到一个文件夹下?
  • java删除文件夹下所有文件示例分享
  • 在java.util.zip中如何处理文件夹
  • JAVA遍历一个文件夹中的所有文件的小例子
  • java mail中增加文件夹的问题,谢谢各位急
  • java创建的文件夹的权限问题!
  • 如和在dos下只打java或appletviewer反正就是bin文件夹中的程序就运行
  • java生成文件夹和文件的简单示例分享
  • Java创建文件夹及文件实例代码
  • java删除文件和文件夹具体实现
  • Java读写文件创建文件夹多种方法示例详解
  • java Servlet获取和设置cookie实例代码
  • 万般火急!关于java打印,已经得到printerJob实例,那么怎么通过它得到Pageable实例?
  • 可以有其他两个类的实例同时调用一个java实例的两个方法吗?
  • <java技术手册>与<java实例技术手册>这两本书怎么样?
  • Java单例模式实例简述
  • 寻求java加密算法及实例
  • java web start实例代码COPY不了,怎么办?
  • 请问哪里有《java实例技术手册》的电子书下载?100分赠送!
  • 请教:JAVA中说什么类的实例,那是怎么样的一个概念呢?
  • java实现大数加法(BigDecimal)的实例代码
  • Java究竟能干些什么呢?清高手们列举一些实例出来,跟帖有分.
  • java HashMap的keyset实例
  • java获取当前日期使用实例
  • java之super关键字用法实例解析
  • Java调用DOS实现定时关机的实例
  • java结束进程的实例代码
  • 急!大家谁有类似visio的java实例或代码?
  • java 如何获取对象实例的大小
  • 高分火速求解,请在线朋友回答:java自定义类怎样生成实例数组?( className[] N=new className[X];怎么不行?)
  • 刚学java想试编一个文本编辑器,各位能不能给推荐一些较好的参考程序或实例
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • java中读取.txt和读取.ini文件的方式和命令是否一样
  • 请问在Java程序中能否直接读取ini文件,如何读取?
  • 高手帮忙:如何用java读取数据(从ftp服务器上一个文本文件)但不采用get的方式直接读取,打印数据内容(web方式)
  • 请问在Linux里,如何用JAVA读取内存里的PATH设置
  • 可以用Java读取Notes的数据么?急,定给分!!!
  • 有没有人晓得怎么用JAVA实现读取UTMP或WTMP等日志文件!!!!!!!!!!!!!
  • 哪里有用JAVA读取 MS-Excel文件的包?谢谢谢谢
  • java自动根据文件内容的编码来读取避免乱码
  • Java读取二维条码
  • java读取csv文件示例分享(java解析csv文件)
  • 如何用JAVA读取Doc文档?(必给分)
  • 急!送分求助,java程序中如何读取系统环境变量?
  • 如何在java应用程序中读取键盘的输入?闪电给分。
  • 请问各位大虾: 如何用java读取一个xml文档
  • 请问如何用Java从数据库中读取媒体文件,如图片,mpeg 等
  • 如何读取java文件?
  • 谁有读取cad文件的java源程序?谢谢了!
  • 哪为做过读取PDF格式文件的JAVA程序
  • 用java读取位图
  • 笨苯问题:java类中如何读取xml中定义的属性信息。
  • java命名空间java.sql类types的类成员方法: java_object定义及介绍
  • 我想学JAVA ,是买THINK IN JAVA 还是JAVA2核心技术:卷1 好???
  • java命名空间java.awt.datatransfer类dataflavor的类成员方法: imageflavor定义及介绍
  • 请问Java高手,Java的优势在那里??,Java主要适合于开发哪类应用程序
  • java命名空间java.lang.management类managementfactory的类成员方法: getcompilationmxbean定义及介绍
  • 如何将java.util.Date转化为java.sql.Date?数据库中Date类型对应于java的哪个Date呢
  • java命名空间java.lang.management接口runtimemxbean的类成员方法: getlibrarypath定义及介绍
  • 谁有电子版的《Java编程思想第二版(Thinking in java second)》和《Java2编程详解(special edition java2)》?得到给分
  • java命名空间java.lang.management接口runtimemxbean的类成员方法: getstarttime定义及介绍
  • 本人想学java,请问java程序员的待遇如何,和java主要有几个比较强的方向
  • java命名空间java.awt.datatransfer类dataflavor的类成员方法: stringflavor定义及介绍


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3