Getting Free Space on Windows Drive from Java w/o JNI

D

Dale

import java.io.*;
import java.util.*;

public class Dir {

// An admitted hack, but I don't want to mess with javah and JNI and
// figuring out how to do the java native interface thing
// (having to compile C/C++, or to have to buy something like that
excelsior thing.
//
// I figured I'd share it on the usenet in case someone has a similar
// "light need" to find free space on the drive without going through
// the GetDiskFreeSpaceEx kernal32.dll thing.
//
// My secondary motive for posting is to get see what various
'modern'
// Windows OS's say when they report free space. I'm going to put
// [cmd.exe /c dir /-c] and [bytes free] in an ini file so the user
// can change it if they are running some other flavor of Windows.

public long getFreeSpace(String driveLetter){
StringBuffer dirOutput = new StringBuffer();
try {
Runtime run = Runtime.getRuntime();
File path = new File(driveLetter);
String[] env = null;
Process p = run.exec("cmd.exe /c dir /-c", env, path);
InputStream in = p.getInputStream();
int character;
while((character = in.read()) != -1) {
dirOutput.append((char)character);
}
in.close();
} catch (IOException e) {
System.out.println("Dir.getFreeSpace Runtime Exec ERROR: " + e);
}

long freeSpace = -1;
String aDirLine = "";
try {
StringTokenizer dirLines = new StringTokenizer(new
String(dirOutput),"\n");
while (dirLines.hasMoreTokens()){
aDirLine = dirLines.nextToken();
int bytesFreeLoc = aDirLine.indexOf("bytes free");
if (bytesFreeLoc > -1 ){
freeSpace = Long.parseLong(aDirLine.substring(0,bytesFreeLoc).trim());
}
}

} catch (NumberFormatException e){
System.out.println("Dir.getFreeSpace Parsing ERROR: with [" +
aDirLine + "] as subject.\n" + e);
}
return freeSpace;
}

public static void main (String[] args){
Dir dir = new Dir();
String driveLetter = "d:";
System.out.println("The free space, in bytes, on " + driveLetter + "
was: " + dir.getFreeSpace(driveLetter));
}
}
 
D

Dale

(e-mail address removed) (Dale) wrote in message
Just to follow up....
This excerpt below works in NT, but not in XP because xp says "7
Dir(s) 12345 bytes free" instead of just "12345 bytes free", like nt.

int bytesFreeLoc = aDirLine.indexOf("bytes free");
if (bytesFreeLoc > -1 ){
freeSpace = Long.parseLong(aDirLine.substring(0,bytesFreeLoc).trim());
}

So I did a substring to get everything before "bytes free" then did a
last index of " " (space) to find the space in beween "Dir(s)" and the
number of bytes.
 
D

Dale

Andrew Thompson said:
On 7 Apr 2004 06:34:03 -0700, Dale wrote:

Did not have to to hunt it down when I
first saw this thread, but wondered if
you'd seen this (long, related) thread
<http://groups.google.com/groups?&[email protected]>

No Andrew, I had not seen that thread. Thanks! Great that the chunks
of code are linked. I wish I had seen that thread, because that would
have saved me a bit of time. That code does pretty much what I came
up with but has a flaw (had to change "int index = 1;" to "int index
= 0;". The likely talked about it in the thread (which I didn't read
- the problem is solved).

As to why not JNI, I didn't want to expend time figuring-out what
tools were required to compile a C program. Also I figured it would be
a pain to have to install the dll if I wanted to run my app on another
machine. I needed free space in my program and I had it 30 minutes
later (15 of that was spent trying to find the post you seem to have
found so easily!).

--Dale--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top