Java programming to get hard disk space

Source: Internet
Author: User
Tags numeric value

Because the forum was asked this question, so I wrote this article. Hope to help you.

Generally speaking, to use Java to get hard disk space, there are 3 ways:

1. Call the system command, and then analyze the results, this method has a strong system-dependent, Linux and win under the separate write program.

Here is a win under the example, after the successful compilation, run Java DiskSpace yourdir (such as C:\)

Import Java.io.BufferedReader; 
Import Java.io.InputStreamReader;
/** * Determine free disk spaces for a given directory by * parsing the output of the dir command.
* This class was inspired by the code in * works only under Windows under certain circumstances.
* Yes, it ' s that shaky.
* Requires Java 1.4 or higher. * @[email PROTECTED] *marco Schmidt/public class DiskSpace {private DiskSpace () {//Prevent instantiation of thi
S Class}/** * return available free disk spaces for directory A. * @[email PROTECTED] dirname name of the directory * @[email PROTECTED] free disk spaces in bytes or-1 if unknown * * Publ IC Static Long Getfreediskspace (String dirname) {try {//guess correct ' dir ' command by looking at the//op
   erating system name String OS = System.getproperty ("Os.name");
   String command; if (Os.equals ("Windows NT") | |
   Os.equals ("Windows Watts")) {command = "cmd.exe/c dir" + dirname; else {command = "command.com/c dir" + DIrname;
   }//Run the dir command on the argument directory name Runtime Runtime = Runtime.getruntime ();
   Process process = NULL;
   Process = runtime.exec (command);
   if (process = = null) {return-1; }//Read the output of the dir command//Only the "The last" is of interest bufferedreader in = new Bufferedrea
   Der (New InputStreamReader (Process.getinputstream ()));
   String Line;
   String freeSpace = null;
   while (line = In.readline ())!= null) {freeSpace = line;
   } if (freeSpace = = null) {return-1;
   } Process.destroy ();
   Remove Dots & commas & leading and trailing whitespace freeSpace = Freespace.trim ();
   FreeSpace = Freespace.replaceall ("\.", "");
   FreeSpace = Freespace.replaceall (",", "");
   string[] items = Freespace.split ("");
   The valid numeric value in items after (!) index 0//is probably the free disk space int index = 1;
while (Index < items.length) {try {     Long bytes = Long.parselong (items[index++]);
    return bytes;
  The catch (NumberFormatException nfe) {}} return-1;
  catch (Exception Exception) {return-1;
}/** * Command line program to print the free diskspace to stdout * for all potential root directories a:\ to Z:\
* (when no parameters are given) * or for those directories (drives) specified as parameters.
   * @[email PROTECTED] args program parameters/public static void Main (string[) args) {if (args.length = 0) {
    for (char c = ' A '; c <= ' Z '; C + +) {String dirname = c + ": \ \";
   System.out.println (dirname + "" + getfreediskspace (dirname)); } else {for (int i = 0; i < args.length i++) {System.out.println (Args[i] + "" + getfreediskspace (
   Args[i])); }   }  } }

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.