Solved

How to determine the free diskspace of my machine

Posted on 2003-11-25
3
250 Views
Last Modified: 2010-03-31
Hello All,

I wanted to write a code that should determine a disk space of the machine

how can i do this?

thanks
0
Comment
Question by:asps
3 Comments
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 500 total points
ID: 9823052
import java.io.*;
import java.util.*;
public class  DiskSpace
{
      public static void main(String[] args) throws Exception
      {
            System.out.println("Hello World!");
            
                  Runtime rt = Runtime.getRuntime();
                  String strDiskSpace = null;
                  Process p = null;
                  String strOS = System.getProperty( "os.name" );
                  try{
                  if( strOS.equals( "Windows NT" )){
                  p = rt.exec("cmd.exe /c dir c:\\" );
                  }else{
                  // Windows 95 , 98
                  p = rt.exec("command.com /c dir c:\\" );
                  }
                  InputStream inStream = p.getInputStream();
                  BufferedReader reader = new BufferedReader( new InputStreamReader( inStream));
                  String strTemp;
                  while( (strTemp = reader.readLine()) != null ){
                  strDiskSpace = strTemp;
                  }
                  System.out.println( strDiskSpace.trim() );
                  StringTokenizer sToken = new StringTokenizer( strDiskSpace.trim() , " " );
                  while( sToken.hasMoreTokens()){
                  System.out.println( "Free Space : " + sToken.nextToken() );
                  }
                  p.destroy();
                  }catch( Exception e ){
                  System.out.println( e.getMessage());
                  }
      }
}




P.S  : this code is copied from http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20088852.html
0
 

Author Comment

by:asps
ID: 9823077
thanks alot man
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 9823090
Unfortunately, the code above will only work on Windoze. On *nix systems you have to deal with local/non-local drives, mount points etc. Can get pretty complicated, but not impossible.

Algorithm is:
Read appropriate 'fstab' file to find volumes and mount points
Execute the 'df' command ('massaged' for each *nix flavor) for each mount point
Parse the output of the 'df' command

Much work, unless you only need to support Windoze, or very few flavors of *nix.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySqlDump not dumping triggers 1 53
null output 3 42
Java: anonymous class 4 37
Print Rhino Java Array in Javascript 1 30
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question