?
Solved

How to determine the free diskspace of my machine

Posted on 2003-11-25
3
Medium Priority
?
253 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

752 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