java分析文件夹空间使用情况,找出最大的文件


import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        String dirPath="C:\\Program Files (x86)";
        File dir=new File(dirPath);
        List<FileEntity> list=new ArrayList<>();
        int maxLength=0;
        for(File file:dir.listFiles()){
            if(file.getName().length()>maxLength)maxLength=file.getName().length();
            list.add(new FileEntity(file.getName(),getSize(file)/1024/1024));
        }
        list.sort((e1,e2)-> Double.compare(e2.size,e1.size));
        for(FileEntity f:list){
            System.out.println(fillEndBlank(f.name,maxLength)+"\t\t\t"+f.size+"MB");
        }
    }

    /**
     * Program Files (x86)                 			37650.148MB
     * Windows                             			19850.994MB
     * hiberfil.sys                        			11847.476MB
     * Program Files                       			8012.231MB
     * pagefile.sys                        			4352.0MB
     * Users                               			3777.679MB
     * Recovery                            			833.728MB
     * Driver                              			620.86MB
     * ProgramData                         			533.196MB
     * AMD                                 			312.174MB
     * code                                			109.792MB
     * swapfile.sys                        			16.0MB
     */
    static class FileEntity{
        String name;//文件名
        double size;//大小单位MB
        public FileEntity(String name,double size){
            this.name=name;
            String tmp=String.valueOf(size);
            if(tmp.contains(".")&&tmp.length()-tmp.indexOf(".")>5)tmp=tmp.substring(0,tmp.indexOf(".")+4);
            this.size=Double.parseDouble(tmp);
        }
    }

    public static double getSize(File file){
        if(file.isFile())return file.length();
        long size=0;
        try {
            for(File f:file.listFiles()){
                size+=getSize(f);
            }
        }catch (Exception e){}
        return size;
    }

    public static String fillEndBlank(String name,int length){
        while (name.length()<length){
            name+=" ";
        }
        return name;
    }


}

结果

.f2202b89-bfc4-44c8-81a5-d6dabe264094                    			390.359MB
ms-dotnettools.csharp-2.0.376-win32-x64                  			353.505MB
.cfabd410-1e8d-4103-b4ce-da46cb6e52c0                    			350.537MB
visualstudioexptteam.vscodeintellicode-1.2.30            			129.049MB
wix.vscode-import-cost-3.3.0                             			44.529MB
wallabyjs.quokka-vscode-1.0.555                          			35.645MB
yuichinukiyama.vscode-preview-server-1.3.0               			25.708MB
.56c6ae02-8d0c-4957-84e8-a681253fd0e9                    			14.828MB
eamodio.gitlens-14.2.1                                   			12.174MB
bengreenier.vscode-node-readme-3.0.2                     			7.827MB
visualstudioexptteam.intellicode-api-usage-examples-0.2.7			7.592MB
zignd.html-css-class-completion-1.20.0                   			7.324MB
ms-azuretools.vscode-docker-1.26.0                       			7.097MB
pflannery.vscode-versionlens-1.5.2                       			6.983MB
roerohan.mongo-snippets-for-node-js-1.3.12               			6.023MB
github.vscode-pull-request-github-0.70.0                 			5.567MB
redhat.vscode-yaml-1.14.0                                			5.095MB
ms-dotnettools.vscode-dotnet-runtime-1.7.0               			5.025MB
msjsdiag.debugger-for-chrome-4.13.0                      			3.881MB
vue.volar-1.8.8                                          			3.75MB
tgreen7.vs-code-node-require-1.10.2                      			3.461MB
chris-noring.node-snippets-1.3.3                         			3.092MB
alexti.intellij-idea-light-theme-0.6.1                   			2.03MB
vue.vscode-typescript-vue-plugin-1.8.8                   			1.875MB
christian-kohler.npm-intellisense-1.4.4                  			1.765MB
formulahendry.vscode-mysql-0.4.1                         			1.738MB
redhat.fabric8-analytics-0.3.10                          			1.69MB
orta.vscode-jest-5.2.3                                   			1.486MB
ms-ceintl.vscode-language-pack-zh-hans-1.81.2023081609   			1.102MB
be5invis.vscode-icontheme-nomo-dark-1.3.6                			0.955MB
ms-vscode.vscode-typescript-tslint-plugin-1.3.4          			0.94MB
dbaeumer.vscode-eslint-2.4.2                             			0.734MB
ecmel.vscode-html-css-1.13.1                             			0.699MB
mostik.jetbrainsicons-1.0.5                              			0.414MB
christian-kohler.path-intellisense-2.8.4                 			0.4MB
editorconfig.editorconfig-0.16.4                         			0.28MB
mikestead.dotenv-1.0.1                                   			0.217MB
persephona.theme-dreamweaverweb-1.6.2                    			0.095MB
akamud.vscode-theme-onedark-2.3.0                        			0.053MB
leizongmin.node-module-intellisense-1.5.0                			0.034MB
mohd-akram.vscode-html-format-0.1.2                      			0.022MB
a-fandino.classgenerator-0.2.0                           			0.01MB

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值