Solved

How to determine the free diskspace of my machine

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 58
Which is the latest version of Apache Tomcatl 3 35
HashMap Vs TreeMap 12 49
Problem to start Neon 20 54
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 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…
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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now