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));
}
}
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));
}
}