This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ahamed Ali
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ahamed Ali
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21193
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic