当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:15:00  来源:本站整理

<b>Java编程得到硬盘空间</b>[Java编程]

赞助商链接



  本文“<b>Java编程得到硬盘空间</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

因为论坛有人问到这个问题,所以就写了这篇文章.但愿对大家有所帮忙.

普通来说,要用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("\.", "");
    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]));
    }
   }
  }
}


  以上是“<b>Java编程得到硬盘空间</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • <b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
  • <b>在 Windows 8 中手动安装语言包</b>
  • <b>五个常见 PHP数据库问题</b>
  • Windows中Alt键的12个高效快速的利用本领介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • <b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
  • <b>Win8系统恢复出来经典的开始菜单的办法</b>
  • <b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
  • <b>Windows 7系统下无线网卡安装</b>
  • <b>为什么 Linux不需求碎片整理</b>
  • <b>Windows 8中删除账户的几种办法(图)</b>
  • <b>教你如安在win7下配置路由器</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .