Import java. Io .*;
Import java. Text .*;
Public class diskroot
{
Public static void main (string ARGs []) throws ioexception
{
System. Out. println ("drive \ t disk capacity \ t used space \ t available space ");
// Obtain all root directory objects
File [] frootlist = file. listroots (); // The listroot () Static Method of the fill class obtains the objects in each root directory.
For (INT I = 0; I <frootlist. length; I ++)
{
String sdrive = frootlist [I]. getabsolutepath ();
Long btotal = frootlist [I]. gettotalspace ();
Long bfree = frootlist [I]. getfreespace ();
Long bused = btotal-bfree;
System. out. println (sdrive + "\ t" + returngb (btotal) + "\ t" + returnmb (bused) + "\ t" + returnmb (bfree ));
}
}
// Convert the number in bytes to GB (retain two decimal places)
Private Static string returngb (double darg)
{
// A decimalformat object can specify the format of a number. # A placeholder indicates that a number is displayed, and no number is required. Instead, the position of the 0 placeholder is used. If there is no number, it must be filled with 0.
Decimalformat df = new decimalformat ("#### 0.00 GB ");
Return DF. Format (darg/(1024.0*1024.0*1024.0 ));
}
// Convert the number in bytes to MB
Private Static string returnmb (double darg)
{
Decimalformat df = new decimalformat ("##### 0 MB ");
Return DF. Format (darg/(1024.0*1024.0 ));
}
}