aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Detect remaining space on a drive" Watch "Detect remaining space on a drive" New topic
Author

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
Greenhorn

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.
mavedrive;
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[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....
Don
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.
brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Detect remaining space on a drive