熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Javascript >> 正文

如何用Java得到硬盤空間

2022-06-13   來源: Javascript 

  一般來講要用java得到硬盤空間種方法:
   調用system的command然後分析得到的結果這種方法有很強的系統依賴性linux下和win下要分別寫程序
  下面是一個win下的例子編譯成功之後運行java Diskspace yourdir(比如c:\)
  import javaioBufferedReader;
  import javaioInputStreamReader;
  
  /**
  * 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 its that shaky
  * Requires Java 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 if unknown
  */
  public static long getFreeDiskSpace(String dirName)
  {
  try
  {
  // guess correct dir command by looking at the
  // operating system name
  String os = SystemgetProperty(osname);
  String command;
  if (osequals(Windows NT) ||
  osequals(Windows ))
  {
  command = cmdexe /c dir + dirName;
  }
  else
  {
  command = /c dir + dirName;
  }
  // run the dir command on the argument directory name
  Runtime runtime = RuntimegetRuntime();
  Process process = null;
  process = runtimeexec(command);
  if (process == null)
  {
  return ;
  }
  // read the output of the dir command
  // only the last line is of interest
  BufferedReader in = new BufferedReader(
  new InputStreamReader(processgetInputStream()));
  String line;
  String freeSpace = null;
  while ((line = inreadLine()) != null)
  {
  freeSpace = line;
  }
  if (freeSpace == null)
  {
  return ;
  }
  processdestroy();
  // remove dots & commas & leading and trailing whitespace
  freeSpace = freeSpacetrim();
  freeSpace = freeSpacereplaceAll(\\ );
  freeSpace = freeSpacereplaceAll( );
  String[] items = freeSpacesplit( );
  // the first valid numeric value in items after(!) index
  // is probably the free disk space
  int index = ;
  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,可以跨平台
  從上下載jconfig.
  下載的包的sample裡有很簡單的例子,如果是要得到磁盤空間的話:
  用FileRegistry.getVolumes()得到DiskVolume
  然後call getFreeSpace()和getMaxCapacity()
  就是這麼簡單..:)
  
  方法三:jni
  這個是解決所有和os相關的操作的萬能利器了.
  例子我也懶得寫了.
  寫一個dll然後call之即可.
  

From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/25405.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.