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. 卷积神经网络(CNN)代码实现(MNIST)解析

    在http://blog.csdn.net/fengbingchun/article/details/50814710中给出了CNN的简单实现,这里对每一步的实现作个说明: 共7层:依次为输入层.C1 ...

  2. caffe可重入单例机制分析

    一个函数可重入是指该函数可以被多个线程同时调用.大多数函数都不是可重如的,因为很多函数会修改静态数据结构里的内容,如果多个线程同时调用,势必破坏共享的静态结构.可以在不改变公共接口的情况下,将一个非重 ...

  3. django项目添加utf-8编码支持中文

    代码中出现中文会报错: Non-ASCII character '...' in file ......models.py on line ......., but no encoding decla ...

  4. Golang进程权限调度包runtime三大函数Gosched、Goexit、GOMAXPROCS

    转自:https://www.cnblogs.com/wt645631686/p/9656046.html runtime.Gosched(),用于让出CPU时间片,让出当前goroutine的执行权 ...

  5. Hadoop 2.2.0安装和配置lzo

    转自:http://www.iteblog.com/archives/992 Hadoop经常用于处理大量的数据,如果期间的输出数据.中间数据能压缩存储,对系统的I/O性能会有提升.综合考虑压缩.解压 ...

  6. Git:git diff 命令详解

    工作目录 vs 暂存区 $ git diff <filename> 意义:查看文件在工作目录与暂存区的差别.如果还没 add 进暂存区,则查看文件自身修改前后的差别.也可查看和另一分支的区 ...

  7. jmeter3.0下载及安装

    http://blog.csdn.net/shizhiailian/article/details/52443169 下载: https://archive.apache.org/dist/jmete ...

  8. MTK LCM的添加

    对于LCM驱动移植,一般分为三部曲: 1.硬件IO口配置: 2.确保LCM背光能够正常点亮: 3.LCM驱动移植: 硬件电路: 1.GPIO配置 打开 mediatek\dct\DrvGen.exe ...

  9. git 同步远程已删除的分支和删除本地多余的分支

    使用git branch -a可以查看本地分支和远程分支情况 但远程分支(红色部分)删除后,发现本地并没有同步过来. 一. 同步本地的远程分支 查看本地分支和追踪情况: git remote show ...

  10. Centos7 Crontab

    Centos7 Crontab # 查看crontab -l # 编辑 crontab -e # 重启 service crond restart ccrontab 编写规则 # 分 时 日 月 周 ...