JAVA获取磁盘空间


需要把磁盘空间显示在页面上,这样就不用去服务器查看了,方便

两个办法

 File file = new File("D:");
        long totalSpace = file.getTotalSpace();
        long freeSpace = file.getFreeSpace();
        long usedSpace = totalSpace - freeSpace;
        System.out.println("总空间大小 : " + totalSpace / 1024 / 1024 / 1024 + "G");
        System.out.println("剩余空间大小 : " + freeSpace / 1024 / 1024 / 1024 + "G");
        System.out.println("已用空间大小 : " + usedSpace / 1024 / 1024 / 1024 + "G");
    }
}

方法2:

 File diskPartition = new File("D:");
        long totalCapacity = diskPartition.getTotalSpace();
        long freePartitionSpace = diskPartition.getFreeSpace();
        long usablePatitionSpace = diskPartition.getUsableSpace();
        System.out.println("**** 以M为单位****\n");
        System.out.println("总空间大小 : " + totalCapacity / (1024 * 1024) + " MB");
        System.out.println("已用空间大小 : " + usablePatitionSpace / (1024 * 1024) + " MB");
        System.out.println("剩余空间大小 : " + freePartitionSpace / (1024 * 1024) + " MB");
        System.out.println("\n**** 以G为单位 ****\n");
        System.out.println("总空间大小 : " + totalCapacity / (1024 * 1024 * 1024) + " GB");
        System.out.println("已用空间大小 : " + usablePatitionSpace / (1024 * 1024 * 1024) + " GB");
        System.out.println("剩余空间大小 : " + freePartitionSpace / (1024 * 1024 * 1024) + " GB");

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM