Displays disk space information for each drive letter in a Windows operating system.

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.