zoukankan      html  css  js  c++  java
  • 列出一个目录中所有文件及大小

    package com.test.tree;
    
    import java.io.File;
    
    /**
     * 列出一个目录中所有的文件和他们的大小
     * @author wyl
     *
     */
    public class FileList {
    
        public void list(File f){ //文件根目录,深度为0
            list(f,0);
        }
        
        /**
         * 
         * @param f
         * @param depth 为了显示文件的层级形式
         */
        public void list(File f, int depth){
            printName(f, depth);
            if(f.isDirectory()){
                File[] files = f.listFiles();
                for(File file:files){
                    list(file, depth+1);
                }
            }
        }
    
        /**
         * 循环打印文件名及文件大小
         * @param f
         * @param depth
         */
        private void printName(File f, int depth) {
            // TODO Auto-generated method stub
            String name = f.getName();
            for(int i=0;i<depth;i++){ //缩进打印文件名
                System.out.print("     ");
            }
            if(f.isDirectory()){
                System.out.println("Dir: " + name);
            }else{
                System.out.println(f.getName() + "" + f.length());
            }
        }
        
        public static void main(String[] args) {
            FileList fileList = new FileList();
            File file = new File("C:/");
            fileList.list(file);
        }
    }
  • 相关阅读:
    关于JVM的一些想法
    hashMap理解以及jdk1.7、jdk1.8其中区别
    各数据库如何实现自增
    dubbo遇坑记录
    mysql建表语句问题
    @Configuration
    生成一个唯一的ID
    门面模式
    关于getClass().getClassLoader()
    元素链
  • 原文地址:https://www.cnblogs.com/studyDetail/p/7154437.html
Copyright © 2011-2022 走看看