知识大全 用JAVA得到硬盘空间的三种不同方法

Posted

篇首语:私心胜者,可以灭公。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 用JAVA得到硬盘空间的三种不同方法相关的知识,希望对你有一定的参考价值。

用JAVA得到硬盘空间的三种不同方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  一般来讲 要用java得到硬盘空间 有 种方法:      调用system的mand 然后分析得到的结果 这种方法有很强的系统依赖性 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 mand   * This class is inspired by the code at  * Works only under Windows under certain circumstances   * Yes it s 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 mand by looking at the   // operating system name  String os = System getProperty( os name );  String mand;  if (os equals( Windows NT ) ||  os equals( Windows ))    mand = cmd exe /c dir + dirName;    else    mand = /c dir + dirName;    // run the dir mand on the argument directory name  Runtime runtime = Runtime getRuntime();  Process process = null;  process = runtime exec(mand);  if (process == null)    return ;    // read the output of the dir mand  // 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 ;    process destroy();  // remove dots & mas & leading and trailing whitespace  freeSpace = freeSpace trim();  freeSpace = freeSpace replaceAll( \\\\ );  freeSpace = freeSpace replaceAll( );  String[] items = freeSpace split( );  // 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 ;    catch (Exception exception)    return ;         /**  * Command line program to print the free diskspace to stdout  * for all 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 == )    for (char c = A ; c <= Z ; c++)    String dirName = c + :\\\\ ;  System out println(dirName + +  getFreeDiskSpace(dirName));      else    for (int i = ; 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之即可 cha138/Article/program/Java/Javascript/201311/25268

相关参考