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. [Canvas]空战游戏进阶 增加己方子弹管理类

    点此下载源码,可用Chrome打开观看. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> <meta http ...

  2. IDEA攻略合辑

    AS使用lombok注解报错:Annotation processors must be explicitly declared now. The following dependencies on ...

  3. 正则双重过滤 /// splitKey1 第一个正则式匹配 /// splitKey2 匹配结果中再次匹配进行替

    /// <summary> /// 正则双重过滤 /// splitKey1 第一个正则式匹配 /// splitKey2 匹配结果中再次匹配进行替换 /// </summary&g ...

  4. Docker在windows下的使用【二】

    可参考学习地址: 极客学院docker教程,还不错,可以参考 1.Dockerhub下载镜像 下载地址:Dockerhub地址 有两种方式可以获得新的镜像 直接从dockerhub下载编译好的imag ...

  5. 3分钟搞明白信用评分卡模型&模型验证

    信用评分卡模型在国外是一种成熟的预测方法,尤其在信用风险评估以及金融风险控制领域更是得到了比较广泛的使用,其原理是将模型变量WOE编码方式离散化之后运用logistic回归模型进行的一种二分类变量的广 ...

  6. 【微信小程序项目实践总结】30分钟从陌生到熟悉 web app 、native app、hybrid app比较 30分钟ES6从陌生到熟悉 【原创】浅谈内存泄露 HTML5 五子棋 - JS/Canvas 游戏 meta 详解,html5 meta 标签日常设置 C#中回滚TransactionScope的使用方法和原理

    [微信小程序项目实践总结]30分钟从陌生到熟悉 前言 我们之前对小程序做了基本学习: 1. 微信小程序开发07-列表页面怎么做 2. 微信小程序开发06-一个业务页面的完成 3. 微信小程序开发05- ...

  7. hdoj:2071

    Max Num Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. golang处理signal

    signal一般用来实现优雅重启,或者重新加载配置文件等操作. 废话不多说,上表格 动作 号码 信号 golang kill pid 15 SIGTERM terminated kill -9 pid ...

  9. r里面如何实现两列数据合并为一列

    library(dplyr) unite(mtcars, "vs_am", vs, am) Merging Data Adding Columns To merge two dat ...

  10. 如何让linux的history命令显示时间记录

    在.bashrc文件追加如下内容即可: HISTFILESIZE= HISTSIZE= HISTTIMEFORMAT='%F %T ' export HISTTIMEFORMAT