Meaningless Drivel is fun!
The moose likes Java in General and the fly likes Detect remaining space on a drive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Detect remaining space on a drive" Watch "Detect remaining space on a drive" New topic

Detect remaining space on a drive

don cline
Ranch Hand

Joined: Oct 10, 2003
Posts: 35
I have a java process that writes out a file. Before I write the file I want to detect how much space there is left.
In Java, how do I detect how much space is left on a drive (without filling that space)?
Maverick Lasa

Joined: Oct 18, 2003
Posts: 23
I don't know if there is already an available API for this one but you can do this thru JNI. Calling the native library such as GetDiskFreeSpace/Ex in Windows.
Joe Ess

Joined: Oct 29, 2001
Posts: 9150

On windows you could do a Runtime.exec() on dir, read the output from the Process's output stream and parse out the last number, which is the free disk space. Linux (and maybe Unix in general) has a specific command, df, for free space. How's that for a kludge?

[How To Ask Questions On JavaRanch]
don cline
Ranch Hand

Joined: Oct 10, 2003
Posts: 35
Many thanks for your responses. I was hoping I could do this without using Runtime.exec(). (It must run on Windows and AIX). Oh, well, I guess I just detect which I am on and write both....
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
There are no built-in Java APIs to do this. You could write some jNI code to make native calls, but that's a hassle and you'd have to write native code for every platform you wanted to support. if you're only supporting 2 OSes, it might be easier to just write a class that does the execing for you.

My Java Freeware:<br />MACCC - <a href="" target="_blank" rel="nofollow"></a><br />Nator - <a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: Detect remaining space on a drive
It's not a secret anymore!