import java.io.File;public class SpaceChecker {
    public static void main(String[] args) {         //File win = new File("C:/");
     File win = new File("D:/");
         System.out.println(win.getPath());
         System.out.println(win.getName());
         System.out.println("Free space = " + win.getFreeSpace());
         System.out.println("Usable space = " + win.getUsableSpace());
         System.out.println("Total space = " + win.getTotalSpace());
         
          Runtime   r   =   Runtime.getRuntime();   
         float   freeMemory   =   (float)   r.freeMemory();   
         float   totalMemory   =   (float)   r.totalMemory();
        
         System.out.println("freeMemory"+ freeMemory);
         System.out.println("totalMemory"+totalMemory);
     }
}
可是我要获取整个电脑硬盘的大小信息咋处理啊?

解决方案 »

  1.   

    你要读取系统的参数。不同的系统的读取方法不一样,
    windows一般读注册表,
      

  2.   

    我jdk1.5.0_11, 上面的代码编译有错误
    win.getFreeSpace(); win.getUsableSpace();win.getTotalSpace() 方法不存在你用的那个jdk 版本啊
      

  3.   

    jdk1.6的 1.6里面有这个方法
      

  4.   

    File[] roots = File.listRoots();
    for (File _file : roots) {
    }
    可以把各个盘符的大小取出来。但是最要命的是把链接服务器的那个硬盘也取出来了。
      

  5.   

    jdk1.6才支持获得磁盘空间大小的方法
      

  6.   


    import java.io.File;
    import java.text.DecimalFormat;public class Test {
    public static void main(String[] args) {
    File[] roots = File.listRoots();
    double constm = 1024 * 1024 * 1024 ;
    double total = 0d;
    for (File _file : roots) {
    System.out.println(_file.getPath());
    System.out.println("剩余空间 = " + doubleFormat(_file.getFreeSpace()/constm)+" G");
    System.out.println("已使用空间 = " + doubleFormat(_file.getUsableSpace()/constm)+" G");
    System.out.println(_file.getPath()+"盘总大小 = " + doubleFormat(_file.getTotalSpace()/constm)+" G");
    System.out.println();
    total+=_file.getTotalSpace();
    }
    System.out.println("你的硬盘总大小 = "+doubleFormat(total/constm));
    }

    public static String doubleFormat(double d){   
    DecimalFormat df = new DecimalFormat("0.##");   
    return df.format(d);                   
    }
    }
      

  7.   

    #9楼 代码适合JDK1.6但在JDK1.5不可行,不知道还有没其它方法
      

  8.   


    usable space: 是可用空间的意思,不是已用空间