• 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;
            }

    }

  • 相关阅读:
    模板元编程实现素数判定
    JDBC开发
    4.9 当相应行存在时更新
    QT5中如何使用QFtp类(这个类虽然没有被收录,但一直在更新)
    gcc和g++的区别
    Awesome C/C++(图像部分)
    Ubuntu更新源
    GO的GDB调试
    内核探测工具systemtap简介
    列举一下项目中使用的产品和技术
  • 原文地址:https://www.cnblogs.com/woshuaile/p/8405261.html
Copyright © 2020-2023  润新知