• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Detect remaining space on a drive

 
don cline
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
don cline
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic