This article shares two codes for obtaining disk space, please refer to it.
Code 1:
import java.io.File;public class DiskSpaceDetail {public static void main(String[] args) {File diskPartition = new File("C:");long totalCapacity = diskPartition.getTotalSpace();long freePartitionSpace = diskPartition.getFreeSpace();long usablePatitionSpace = diskPartition.getUsableSpace();System.out.println("**** Sizes in Mega Bytes ****/n");System.out.println("Total C partition size : " + totalCapacity / (1024*1024) + " MB");System.out.println("Usable Space : " + usablePatitionSpace / (1024 *1024) + " MB");System.out.println("Free Space : " + freePartitionSpace / (1024 *1024) + " MB");System.out.println("/n**** Sizes in Giga Bytes ****/n");System.out.println("Total C partition size : " + totalCapacity / (1024*1024*1024) + " GB");System.out.println("Usable Space : " + usablePatitionSpace / (1024 *1024*1024) + " GB");System.out.println("Free Space : " + freePartitionSpace / (1024 *1024*1024) + " GB");}}Running results
Code 2:
public class FreeDiskSpace {public static void main(String[] args) {File file = new File("c:");long totalSpace = file.getTotalSpace();long freeSpace = file.getFreeSpace();long usedSpace = totalSpace - freeSpace;System.out.println("Total space size: " + totalSpace / 1024 / 1024 / 1024 + "G");System.out.println("Remaining space size: " + freeSpace / 1024 / 1024 / 1024 / 1024 / 1024 + "G");System.out.println("Used space size: " + usedSpace / 1024 / 1024 / 1024 + "G");}}result:
Summarize
Haha, it made everyone laugh.
The above is the entire content of the two code examples of this article about Java obtaining disk space. I hope it will be helpful to everyone. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!