import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class DocCountSize {
     public static void main(String[] args) {
        
            List<Bean> data = new ArrayList<Bean>();
            //输入一个路径
            String path="C:\\\\Program Files\\\\Java\\\\jre1.8.0_131\\\\bin\\\\dtplugin";
            getFile(data, path);    
            save(data);
            System.out.println("统计完毕");
        }
    
         
        private static void save(List<Bean> data) {
            OutputStream os = null;
            OutputStreamWriter osw = null;
            try {
                //将统计的数据大小统计为文件     路径
                os = new FileOutputStream(new File("C:/d1.csv"));
                osw = new OutputStreamWriter(os, "GBK");
                for (Bean bean : data) {
                    osw.write(bean + "\r\n");
                }
                osw.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (osw != null) {
                    try {
                        osw.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        private static void getFile(List<Bean> data, String path) {
    
            File f = new File(path);
            File[] fs = f.listFiles();
      
            if (fs == null) {
                return;
            }
            for (File file : fs) {
                //将统计的文件的字节数   转换为k  方便计算大小
                if (file.isFile()) {
                    data.add(new Bean(file.getParentFile().getAbsolutePath(), file.getName(), (int) (getFileSize(file)/1024)));
                } else {
                    getFile(data, file.getAbsolutePath());
                }
            }
        }
    
        private static int getFileSize(File f) {
            InputStream fis = null;
            try {
                fis = new FileInputStream(f);
                int docsize=fis.available();
                return docsize;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            return 0;
        }
    }
    
    class Bean {
        private String filePath;
        private String fileName;
        private int size;
    
        public Bean(String filePath, String fileName, int size) {
            this.filePath = filePath;
            this.fileName = fileName;
            this.size = size;
        }
    
        public String getFilePath() {
            return filePath;
        }
    
        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }
    
        public String getFileName() {
            return fileName;
        }
    
        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
    
        public int getSize() {
            return size;
        }
    
        public void setSize(int size) {
            this.size = size;
        }
    
//        public String toString() {
//            return filePath + "," + fileName + "," + size + "K";
//        }
    
        public String toString() {
            return filePath + "," + fileName + "," + size;
        }

}

Java 读取某文件下的所有文件的大小 并将所有文件的文件名,以及对应大小输出在xls表格里的更多相关文章

  1. java读取resource目录下的配置文件

    java读取resource目录下的配置文件 1:配置resource目录 下的文件 host: 127.0.0.1 port: 9300 2:读取    / 代表resource目录 InputSt ...

  2. java 读取固定目录下的文件(和上篇差点儿相同)

    package gao.org; import java.io.FileNotFoundException; import java.io.IOException; import java.io.Fi ...

  3. Java 读取指定目录下的文件名和目录名

    需求:读取指定目录下的文件名和目录名 实现如下: package com.test.common.util; import java.io.File; public class ReadFile { ...

  4. Java读取WEB-INF目录下的properties配置文件

    如何在Java代码中读取WEB-INF目录下的properties配置文件,下文给出了一个解决方案. 我们习惯将一些配置信息写在配置文件中,比如将数据库的配置信息URL.User和Password写在 ...

  5. Java 读取某个目录下所有文件、文件夹

    /** * @Author: * @Description:获取某个目录下所有直接下级文件,不包括目录下的子目录的下的文件,所以不用递归获取 * @Date: */ public static Lis ...

  6. java读取项目路径下的中文文件乱码问题

    出现乱码错误: 处理方案: 对文件路径中存在中文的,都要进行URLDecoder.decode(path,"UTF-8")编码转换 wordContent = URLEncoder ...

  7. java读取指定package下的所有class

     JAVA如何扫描一个包下面的所有类,并加载到内存中去? spring中有一个<context:component-scan base-package="com.controller& ...

  8. Java读取Maven工程下的配置文件,工具类

    Java开发中,经常需要在maven工程中读取src/main/resources下的配置文件: 思路如下: Class.getClassLoader() 返回类加载器ClassLoader,进而可以 ...

  9. Java读取maven目录下的*.properties配置文件

    public class ReadProperties{ private static String proFileName = "/config/MQSubjectId.propertie ...

随机推荐

  1. 【.NET 深呼吸】.net core 中的轻量级 Composition

    记得前面老周写过在.net core 中使用 Composition 的烂文.上回老周给大伙伴们介绍的是一个“重量级”版本—— System.ComponentModel.Composition.应该 ...

  2. centos7 安装curl-7.51.0

    curl简介curl是一个广泛使用的用来上传和下载的命令行工具,当然严格来讲,它还可以有别的用途.对于测试来讲,它是Web相关测试非常实用的工具,包括debugging,使用起来非常方便.而且另一方面 ...

  3. Sql Server 增加字段、修改字段、修改类型、修改默认值(转)

    转:http://www.cnblogs.com/pangpanghuan/p/6432331.html Sql Server 增加字段.修改字段.修改类型.修改默认值 1.修改字段名: alter ...

  4. Linux Crontab及使用salt进行管理

    一.引言: 最近无意之间看到salt有一个cron的模块,今天就在这里介绍linux crontab以及通过salt的cron对crontab的管理. 二.Linux crontab的介绍: cron ...

  5. 微表面分布函数(Microfacet Distribution Function)确切含义

    <Physically-Based Shading Models in Film and Game Production>中说:“D()的值不局限于0到1,可以任意大”,这句话使我比较好奇 ...

  6. 为github公开项目单独设置用户名

    背景: 由于在公司git的配置是公司内自己的用户id及邮箱,如果用同样的配置在github上提交公开项目,可能造成用户名及邮箱泄露,因此对于github上的项目,最好单独设置用户名,而每次在向gith ...

  7. 【Java】类加载过程

    JVM把class文件加载到内存,并对数据进行校验.解析和初始化,最终形成JVM可以直接使用的Java类型的过程. 类加载的过程主要分为三个部分: 加载 链接 初始化 而链接又可以细分为三个小部分: ...

  8. Centos7下安装Oracle11g r2图形化界面数据库

    我的centos7是在VMware下安装的,安装Oracle安装了好久好久,最开始的时候在网上找的两个文章,按照步骤装,有一篇写着装的时候有灰色的竖线,直接按space键或者鼠标右键closed关闭掉 ...

  9. Cordova Error: cmd: Command failed with exit code ENOENT

    执行Cordova platform add android时提示:Error: cmd: Command failed with exit code ENOENT. 网上搜索后得到如下结果: 比对着 ...

  10. 匿名函数gc分析

    测试一:使用member function创建action会产生gc,不管该函数是否访问外部变量: private System.Action memberAct = null; // gc 112B ...