aspose file tools*
The moose likes Java in General and the fly likes How do I calculate the free disk space from within a java program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do I calculate the free disk space from within a java program?" Watch "How do I calculate the free disk space from within a java program?" New topic
Author

How do I calculate the free disk space from within a java program?

Ahamed Ali
Greenhorn

Joined: Apr 21, 2005
Posts: 19
Hi all,

Someone could show me a simple java program to calculate the disk drive (e.g. drive C space on windows 2000.

Thanks in advance.


Sharief
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There's nothing in Java to help you. You'll have to go outside the JVM to a batch or shell script that writes a value or a JNI call to an OS API. Let me know if none of those things made sense.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ahamed Ali
Greenhorn

Joined: Apr 21, 2005
Posts: 19
I have tried the following code, but it couldn't run

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

class DiskSpace {
public static void main(String[] arg) {
BufferedReader r;
try {
r = new BufferedReader(
new InputStreamReader(
Runtime.getRuntime().exec(
arg.length>0?"df -v "+arg[0]:"df -v"
).getInputStream()
) );
String s = r.readLine();
while (s != null) {
// change this to parse s
System.out.println(s);
//
s = r.readLine();
}
} catch (IOException ex) {
ex.printStackTrace();
System.exit(0);
}
}
}

Let me know someone could help me.

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

From your question, I thought you were using Windows... I don't think the "df" command works in Windows. Not unless it is an NT thing that I don't know about.

In any case, you can pull the disk free from the following xp/nt command. You still have to parse the results though.

C:\> typeperf -sc 1 "logicaldisk(c \free megabytes"

Henry
[ April 22, 2005: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I calculate the free disk space from within a java program?