Solved

How to determine the free diskspace of my machine

Posted on 2003-11-25
3
247 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now