国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何用Java得到硬盤空間

2019-11-18 13:12:54
字體:
來源:轉載
供稿:網友

  一般來講,要用java得到硬盤空間,有3種方法:
  1. 調用system的command,然后分析得到的結果,這種方法有很強的系統依靠性,linux下和win下要分別寫程序
  下面是一個win下的例子,編譯成功之后,運行java Diskspace yourdir(比如c:/)
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  
  /**
  * Determine free disk space for a given Directory by
  * parsing the output of the dir command.
  * This class is inspired by the code at
  * Works only under Windows under certain circumstances.
  * Yes, it's that shaky.
  * Requires Java 1.4 or higher.
  * @[EMAIL PROTECTED]
  *Marco Schmidt
  */
  public class Diskspace
  {
  private Diskspace()
  {
  // prevent instantiation of this class
  }
  
  /**
  * Return available free disk space for a directory.
  * @[EMAIL PROTECTED]
  dirName name of the directory
  * @[EMAIL PROTECTED]
  free disk space in bytes or -1 if unknown
  */
  public static long getFreeDiskSpace(String dirName)
  {
  try
  {
  // guess correct 'dir' command by looking at the
  // Operating system name
  String os = System.getProperty("os.name");
  String command;
  if (os.equals("Windows NT")
  os.equals("windows 2000"))
  {
  command = "cmd.exe /c dir " + dirName;
  }
  else
  {
  command = "command.com /c dir " + dirName;
  }
  // run the dir command on the argument directory name
  Runtime runtime = Runtime.getRuntime();
  Process process = null;
  process = runtime.exec(command);
  if (process == null)
  {
  return -1;
  }
  // read the output of the dir command
  // only the last line is of interest
  BufferedReader in = new BufferedReader(
  new InputStreamReader(process.getInputStream()));
  String line;
  String freeSpace = null;
  while ((line = in.readLine()) != null)
  {
  freeSpace = line;
  }
  if (freeSpace == null)
  {
  return -1;
  }
  process.destroy();
  // remove dots & commas & leading and trailing whitespace
  freeSpace = freeSpace.trim();
  freeSpace = freeSpace.replaceAll("http://.", "");
  freeSpace = freeSpace.replaceAll(",", "");
  String[] items = freeSpace.split(" ");
  // the first valid numeric value in items after(!) index 0
  // is probably the free disk space
  int index = 1;
  while (index < items.length)
  {
  try
  {
  long bytes = Long.parseLong(items[index++]);
  return bytes;
  }
  catch (NumberFormatException nfe)
  {
  }
  }
  return -1;
  }
  catch (Exception exception)
  {
  return -1;
  }
  }
  
  /**
  * Command line program to print the free diskspace to stdout
  * for all 26 potential root directories A:/ to Z:* (when no parameters are given to this program)
  * or for those directories (drives) specified as parameters.
  * @[EMAIL PROTECTED]
  args program parameters
  */
  public static void main(String[] args)
  {
  if (args.length == 0)
  {
  for (char c = 'A'; c <= 'Z'; c++)
  {
  String dirName = c + "://";
  System.out.println(dirName + " " +
  getFreeDiskSpace(dirName));
  }
  }
  else
  {
  for (int i = 0; i < args.length; i++)
  {
  System.out.println(args[i] + " " +
  getFreeDiskSpace(args[i]));
  }
  }
  }
  }
  
  方法二:使用Jconfig,可以跨平臺
  從http://www.tolstoy.com/samizdat/jconfig.Html上下載jconfig.
  下載的包的sample里有很簡單的例子,假如是要得到磁盤空間的話:
  用FileRegistry.getVolumes()得到DiskVolume
  然后call getFreeSpace()和getMaxCapacity()
  就是這么簡單..:)
  
  方法三:jni
  這個是解決所有和os相關的操作的萬能利器了.
  例子我也懶得寫了.
  寫一個dll然后call之即可.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗甸县| 甘谷县| 华容县| 萨迦县| 峨边| SHOW| 清河县| 古浪县| 怀仁县| 兴国县| 湄潭县| 阿拉尔市| 柘荣县| 元谋县| 宜宾县| 灵山县| 三台县| 达孜县| 溧水县| 临湘市| 奎屯市| 保靖县| 密云县| 光山县| 谢通门县| 慈利县| 中卫市| 太谷县| 梅河口市| 沙河市| 康乐县| 独山县| 邛崃市| 金湖县| 沂水县| 嵊泗县| 洛隆县| 新源县| 凤阳县| 濮阳市| 五指山市|