java可以检查硬盘大小吗?

[小文] 2010-08-04 09:54:20
如题java可以得到硬盘大小吗?可以得到硬盘剩余容量吗?
...全文
468 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lognic10 2010-08-18
  • 打赏
  • 举报
回复
我也加个简单的吧
import java.io.*;

public class TestRootSpace{
static long total = 0;
public static void main(String[] args){
File[] roots = File.listRoots();
File f;

for(int i=0;i<roots.length;i++){
f = roots[i];
System.out.print("盘符:" + f);
System.out.print("\t总大小:" + f.getTotalSpace()/(1024*1024*1024) + "GB\t");
System.out.print("剩余大小:" + f.getFreeSpace()/(1024*1024*1024) + "GB");
System.out.println();
total += f.getTotalSpace();
}
System.out.print("硬盘总大小:");
System.out.println(total/(1024*1024*1024) + "GB");
}
}
luozhangwen 2010-08-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yktd26 的回复:]

[/Quote]

稍微修改了下.


package com.lzw;

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") || os.equals("Windows XP"))
{
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]));
}
}
}

}
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dexter701 的回复:]

jdk1.5能够得到硬盘大小吗?
[/Quote]

不能!
[小文] 2010-08-17
  • 打赏
  • 举报
回复
jdk1.5能够得到硬盘大小吗?
cyuan09 2010-08-05
  • 打赏
  • 举报
回复
用JNI吧
z_free 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 redduke1202 的回复:]
java.io.File

getTotalSpace
public long getTotalSpace()返回此抽象路径名指定的分区大小

getFreeSpace
public long getFreeSpace()返回此抽象路径名指定的分区中未分配的字节数。

listRoots
public static File[] listRoots()列出可用的文件系统根。

……
[/Quote]
支持5楼
zuoguodang 2010-08-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 redduke1202 的回复:]
java.io.File

getTotalSpace
public long getTotalSpace()返回此抽象路径名指定的分区大小

getFreeSpace
public long getFreeSpace()返回此抽象路径名指定的分区中未分配的字节数。

listRoots
public static File[] listRoots()列出可用的文件系统根。

……
[/Quote]
这个是可以的
「已注销」 2010-08-04
  • 打赏
  • 举报
回复
java.io.File

getTotalSpace
public long getTotalSpace()返回此抽象路径名指定的分区大小

getFreeSpace
public long getFreeSpace()返回此抽象路径名指定的分区中未分配的字节数。

listRoots
public static File[] listRoots()列出可用的文件系统根。

遍历所有逻辑磁盘,然后获取每个磁盘的总大小及剩余大小
相加即可

since JDK 1.6
dahai1987102 2010-08-04
  • 打赏
  • 举报
回复
批处理,不错的想法
yktd26 2010-08-04
  • 打赏
  • 举报
回复
windows
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]));
   }
  }
 }
}

宅男小何 2010-08-04
  • 打赏
  • 举报
回复
可以执行cmd命令啊、。。。
龙四 2010-08-04
  • 打赏
  • 举报
回复
纯java不可以

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧