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

 

猜你喜欢

转载自www.cnblogs.com/jnhs/p/10163419.html