this is java code for check free space in partition..
private static final Pattern NON_DIGIT = Pattern.compile("(([^0-9]*))");
private static final Pattern WORDS = Pattern.compile("(\\s*(\\S+)\\s*)");
public static long getFreeSpace(File file) {
final String osName = System.getProperty("os.name");
try {
if (osName.indexOf("Windows") != -1) {
return freeSpaceWindow(file);
} else if (osName.equals("Linux")) {
return freeSpaceUnix(file);
} else {
System.err.println("Unknown os=" + osName);
return freeSpaceUnix(file);
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
return -1;
}
} // end getFreeSpace
private static long freeSpaceUnix(File file) throws IOException {
Process process = Runtime.getRuntime().exec("df");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
List vectorinfos = new ArrayList(8);
while ((line = reader.readLine()) != null) {
if (line.charAt(0) == '/') {
vectorinfos.add(WORDS.split(line));
} // end if line.startwith("/")
} // end while
for (int i = vectorinfos.size() - 1; i >= 0; i--) {
String[] info = (String[]) vectorinfos.get(i);
if (file.getAbsolutePath().indexOf(info[5]) != -1) {
return Long.parseLong(info[3]);
} // end if
} // end for
return -1;
}
private static long freeSpaceWindow(File file) throws IOException {
Process process = Runtime.getRuntime().exec("cmd /c dir /-c " + file.getAbsolutePath());
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null && !line.endsWith("libres") && !line.endsWith("free")) { }
if (line == null) {
return -1;
}
line = line.substring(line.lastIndexOf(")") + 1);
Matcher matcher = NON_DIGIT.matcher(line);
line = matcher.replaceAll("");
return Long.parseLong(line);
}
private static final Pattern NON_DIGIT = Pattern.compile("(([^0-9]*))");
private static final Pattern WORDS = Pattern.compile("(\\s*(\\S+)\\s*)");
public static long getFreeSpace(File file) {
final String osName = System.getProperty("os.name");
try {
if (osName.indexOf("Windows") != -1) {
return freeSpaceWindow(file);
} else if (osName.equals("Linux")) {
return freeSpaceUnix(file);
} else {
System.err.println("Unknown os=" + osName);
return freeSpaceUnix(file);
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
return -1;
}
} // end getFreeSpace
private static long freeSpaceUnix(File file) throws IOException {
Process process = Runtime.getRuntime().exec("df");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
List vectorinfos = new ArrayList(8);
while ((line = reader.readLine()) != null) {
if (line.charAt(0) == '/') {
vectorinfos.add(WORDS.split(line));
} // end if line.startwith("/")
} // end while
for (int i = vectorinfos.size() - 1; i >= 0; i--) {
String[] info = (String[]) vectorinfos.get(i);
if (file.getAbsolutePath().indexOf(info[5]) != -1) {
return Long.parseLong(info[3]);
} // end if
} // end for
return -1;
}
private static long freeSpaceWindow(File file) throws IOException {
Process process = Runtime.getRuntime().exec("cmd /c dir /-c " + file.getAbsolutePath());
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null && !line.endsWith("libres") && !line.endsWith("free")) { }
if (line == null) {
return -1;
}
line = line.substring(line.lastIndexOf(")") + 1);
Matcher matcher = NON_DIGIT.matcher(line);
line = matcher.replaceAll("");
return Long.parseLong(line);
}
Comments