Java获取磁盘大小

我们在网上查找获取磁盘大小的程序时,经常会见到这样的程序,windows是一套写法(用java的api),linux是一套写法(用的是模拟命令行运行命令的方式获取磁盘空间)。所以我们想当然的以为,windows的写法在linux上行不通。一般的写法如下:

package com.example.demo.TypeTest;

import java.io.File;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

/**
 *@Description: TODO
 *@Author: xushu
 *@Date: 2019-10-23 14:34
 **/
public class Test {


    /**
     * 执行系统命令
     *
     * @param cmd 命令
     * @return 字符串结果
     */
    private static String runCommand(String cmd) {
        StringBuilder info = new StringBuilder();
        try {
            Process pos = Runtime.getRuntime().exec(cmd);
            pos.waitFor();
            InputStreamReader isr = new InputStreamReader(pos.getInputStream());
            LineNumberReader lnr = new LineNumberReader(isr);
            String line;
            while ((line = lnr.readLine()) != null) {
                info.append(line).append("\n");
            }
        } catch (Exception e) {
            info = new StringBuilder(e.toString());
        }
        return info.toString();
    }


    /**
     * 判断系统是否为windows
     *
     * @return 是否
     */
    private static boolean isWindows() {
        return System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS");
    }

    private static final String path = "D:/FileFromFtp";

    /**
     * 判断是否需要删除文件
     *
     * @return 磁盘使用率
     */
    public static boolean getWinDiskStoresInfo() {
        //系统为windows
        if(isWindows()) {
            String dirName = path.substring(0, path.indexOf("/") + 1);
            File win = new File(dirName);
            if (win.exists()) {
                long total = win.getTotalSpace();
                long usableSpace = win.getUsableSpace();
                if((double)(total - usableSpace) / total > 0.8){
                    return true;
                }
            }
        }else {
            //系统为Unix
            String ioCmdStr = "df -h /";
            String resultInfo = runCommand(ioCmdStr);
            String[] data = resultInfo.split(" +");
            double total = Double.parseDouble(data[10].replace("%", ""));
            if(total / 100 > 0.8){
                return true;
            }
        }

        return false;
    }



    public static void main(String[] args) throws NoSuchFieldException {
        getWinDiskStoresInfo();
    }
}

其实这样好蛋疼,我也不知道这是什么瞎几把写法,好多人都不知道思考或者实验一下,拿起代码就是抄。Java在windows平台上实现了一套文件系统,为WinNTFileSystem,它支持获取目标卷的总容量和剩余容量。那么在linux上,它同样实现了一套文件系统,为UnixFileSystem,所以,不管什么平台,只需要统一的写法。所以写法可以省略为:

package com.example.demo.TypeTest;

import java.io.File;

/**
 *@Description: TODO
 *@Author: xushu
 *@Date: 2019-10-24 10:59
 **/
public class BBB {

    public static void main(String[] args) {
        File win = new File("/");
        if (win.exists()) {
            long total = win.getTotalSpace();
            long usableSpace = win.getUsableSpace();
            System.out.println((double)(total - usableSpace) / total);
        }
    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,822评论 0 10
  • 1.描述计算机的组成及其功能 (一)计算机的组成 1.CPU 2.CPU风扇 3.BIOS 4.内存 5.硬盘 6...
    whamai阅读 1,506评论 0 1
  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 3,926评论 0 5
  • Linux系统一般有4个主要部分: 内核、shell、文件系统和应用程序。内核、shell和文件系统一起形成了基本...
    偷风筝的人_阅读 3,281评论 1 17
  • Linux系统一般有4个主要部分:内核、shell、文件系统和应用程序。 内核、shell和文件系统一起形成了基本...
    请爱护小动物阅读 2,905评论 0 22