Java获取windows根目录卷标

/**
     * 获取根目录卷标
     * 
     * 通过执行window cmd命令获取返回信息后截取字符串获得
     * 目前测试支持中文XP、英文2003Server(32位和64位)
     * win7还没有测过
     * 
     * @param filePath
     * @return
     */
    private String getVol(String filePath)
    {
        String dir = filePath.replace("/", "");
        String cmdStr = "cmd /c vol " + dir;
        String volStr = "";
        try
        {
            String strTemp;
            InputStream in = Runtime.getRuntime().exec(cmdStr).getInputStream();
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(in, "GBK"));
            while ((strTemp = bufferedReader.readLine()) != null)
            {
                volStr += strTemp;
            }
            /*
             * 中文操作系统
             */
            if (volStr.startsWith(" 驱动器") && !volStr.contains("没有标签"))
            {
                return volStr.substring(volStr.indexOf("中的卷是") + 4,
                        volStr.indexOf("卷的序列号是")).trim()
                        + "(" + dir + ")";
            }
            /*
             * 英文操作系统
             */
            else if (volStr.startsWith(" Volume in drive")
                    && !volStr.contains("has no label"))
            {
                return volStr.substring(volStr.indexOf("is") + 2,
                        volStr.indexOf("Volume Serial Number")).trim()
                        + "(" + dir + ")";
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return dir;
        }
        return dir;
    }
  /**
     * 根目录卷标map
     * 
     * @return
     */
    public Map<String, String> getVolMap()
    {
        Map<String, String> volMap = new HashMap<String, String>();
        File[] files = File.listRoots();
        for (File f : files)
        {
            String path = f.getPath().replaceAll("\\\\", "/");
            volMap.put(path, getVol(path));
        }
        return volMap;
    }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值