Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

DISK SPACE CALCULATION

  Asked By: Willie    Date: Mar 22    Category: Java    Views: 775
  

In java.lang package you have a class known as Runtime.

This Runtime class provides two methods namely freeMemory() , totalMemory().

Using freeMemory() method you can calculate the free disk space in your system.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Muhammad Evans     Answered On: Mar 22

I don't know which version of java  documentation you have but mine says:

<quote_jdk_1.3>
freeMemory

public long freeMemory()

Returns the amount of free  memory in the system. Calling the gc method  may
result in increasing the value returned by freeMemory.

Returns:
an approximation to the total amount of memory currently available for
future allocated objects, measured in bytes.
</quote_jdk_1.3>

But you can use the Runtime class  and use exec to call for example a dos
command and then grab the output
and parse it. Nasty, but AFAIK there is no other way.

 
Answer #2    Answered By: Kian Evans     Answered On: Mar 22

pls note that the runtime class  methods do not provide the hard
disk space  of the system, but give info on physical memory available to java
programs for execution.
pls do not post messages until u have complete idea, this cud mislead
newbies..

 
Answer #3    Answered By: Tomas Thompson     Answered On: Mar 22

I got it
Here is the code,
import java.io.*;
import java.util.*;

public class  GetDiscSpace
{
public static void main( String[] str )
{
Runtime rt = Runtime.getRuntime();
String strDiskSpace = null;
Process p = null;
String strOS = System.getProperty( "os.name" );
try
{
if( strOS.equals( "Windows 2000" ))
{
p = rt.exec("cmd.exe /c dir d:\\" );
}
else
{
// Windows xp
p = rt.exec("cmd.exe /c dir d:\\" );
}

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() , " " );

if( sToken.hasMoreTokens())
{
System.out.println( "Free Space : " +
sToken.nextToken() );
}

p.destroy();
}
catch( Exception e )
{
System.out.println( e.getMessage());
}
}
}

 
Didn't find what you were looking for? Find more on DISK SPACE CALCULATION Or get search suggestion and latest updates.




Tagged: