服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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]));
   }
  }
 }
}

  方法二:使用jconfig,可以跨平台

  从http://www.tolstoy.com/samizdat/jconfig.html上下载jconfig。

  下载的包的sample里有很简单的例子,如果是要得到磁盘空间的话:

  用fileregistry.getvolumes()得到diskvolume。

  然后call getfreespace()和getmaxcapacity()。

  就是这么简单。

  方法三:jni

  这个是解决所有和os相关的操作的万能利器了。

  例子我也懒得写了。

  写一个dll然后call之即可。

扫描关注微信公众号