日期:2014-05-17 浏览次数:20747 次
/** * 根据指定的分号号获得当前磁盘可用字节数 * * @param diskVol * 例:"d:" * @return * @throws IOException */ public Long getVolCapacity(String diskVol) throws IOException { Long capacity = 0L; String[] commands = new String[] { "cmd.exe", "/C", "dir /-c " + diskVol }; Process process; try { // 调用命令行命令并返回命令行进程 process = Runtime.getRuntime().exec(commands); } catch (IOException e) { return capacity; } // 获得返回的进程的输入流 InputStream in = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line = null; List<String> lines = new ArrayList<String>(); while ((line = reader.readLine()) != null) { lines.add(line); } if (lines != null) { line = lines.get(lines.size() - 1); int endPos = line.length(); while (endPos-- >= 0) { if (Character.isDigit(line.charAt(endPos))) { break; } } int startPos = endPos - 1; while (startPos-- >= 0) { if (!Character.isDigit(line.charAt(startPos)) && line.charAt(startPos) != ',' && line.charAt(endPos) != '.') { break; } } capacity = Long.valueOf(line.substring(startPos + 1, endPos + 1)); } return capacity; }