Get the total space of this partition in Java



The method java.io.File.getTotalSpace() is used to obtain the total space in the form of bytes for the partition specified by the required abstract path name. This method requires no parameters and it returns the bytes for the partition i.e. its total space.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.io.File;
public class Demo {
   public static void main(String[] args) {
      File[] roots = File.listRoots();
      try {
         for(File r : roots) {
            System.out.println(r);
            System.out.println("Total space = " + r.getTotalSpace());
         }
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

The output of the above program is as follows −

Output

C:\
Total space = 391309684736
D:\
Total space = 83885027328
E:\
Total space = 23791136768
F:\
Total space = 0

Note − The output may vary on Online Compilers.

Now let us understand the above program.

The method java.io.File.getTotalSpace() is used to obtain the total space in the form of bytes for the partition. A code snippet that demonstrates this is given as follows −

File[] roots = File.listRoots();
try {
   for(File r : roots) {
      System.out.println(r);
      System.out.println("Total space = " + r.getTotalSpace());
   }
} catch(Exception e) {
   e.printStackTrace();
}
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements