JAVA_获取Java所有系统信息_磁盘使用空间_内存信息_网速测试_获取IP地址

    System.out.println("======获取Java所有系统信息======");
        Properties os = System.getProperties();
        for (int i = 0; i < os.keySet().size(); i++) {
            String key = (String) os.keySet().toArray()[i];
            System.out.println(key + "\t\t" + os.getProperty(key));
        }

        System.out.println("=============获取磁盘使用空间=================");
        File[] file = File.listRoots();
        for (File file2 : file) {
            System.out.println(file2.getPath() + "的总空间:" + file2.getTotalSpace() / 1024 / 1024 / 1024 + "GB");
            System.out.println(file2.getPath() + "的已用空间:"
                    + (file2.getTotalSpace() - file2.getFreeSpace()) / 1024 / 1024 / 1024 + "GB");
            System.out.println(file2.getPath() + "的可用空间:" + file2.getFreeSpace() / 1024 / 1024 / 1024 + "GB");
            System.out.println("\r\n");
        }

        System.out.println("=============获取内存信息=================");

        int kb = 1024;
        // 可使用内存
        long totalMemory = Runtime.getRuntime().totalMemory() / kb;
        // 剩余内存
        long freeMemory = Runtime.getRuntime().freeMemory() / kb;
        // 最大可使用内存
        long maxMemory = Runtime.getRuntime().maxMemory() / kb;
        System.out.println(totalMemory);
        System.out.println(freeMemory);
        System.out.println(maxMemory);
        System.out.println("=============获取服务器IP=================");
        try {
            System.out.println("服务器地址:" + InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("=============服务器网速测试=================");
        String con;
        try {

            byte[] bt = new byte[1024];
            InputStream ops = Runtime.getRuntime().exec("ping www.sohu.com -t").getInputStream();
            int s = ops.read(bt);
            int time = 0;
            if ((s ==60)) {

                System.out.println("Request Time Out!");

            } else {
                while ((s = ops.read(bt)) > -1) {
                    con = new String(bt, 0, s);
                    Pattern a = Pattern.compile("\\d{0,4}m");
                    Matcher b = a.matcher(con);

                    while (b.find()) {
                        time = Integer.parseInt((b.group(0).split("m")[0]));

                        System.out.println("服务器当前网速:" + (32 / time + "kb/s"));
                    }
                }
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }

你可能感兴趣的:(JAVA)