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

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 + " ");
                }
                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;
        }

}

原文地址:https://www.cnblogs.com/woshuaile/p/8405261.html