?
Solved

How to determine the free diskspace of my machine

Posted on 2003-11-25
3
Medium Priority
?
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

650 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