溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Windows如何使用java過程獲取操作系統(tǒng)磁盤以及內(nèi)存信息

發(fā)布時間:2021-11-10 09:28:53 來源:億速云 閱讀:265 作者:小新 欄目:關(guān)系型數(shù)據(jù)庫

這篇文章給大家分享的是有關(guān)Windows如何使用java過程獲取操作系統(tǒng)磁盤以及內(nèi)存信息的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

內(nèi)存:先建立一個表, 然后調(diào)用操作系統(tǒng)命令獲取內(nèi)存容量等信息
create table os_meminfo(info VARCHAR2(4000))
/
create or replace and compile java source named syscmd as
import java.io.*;
import java.lang.*;
import oracle.sql.*;
import java.io.BufferedInputStream;  
import java.io.BufferedReader;  
import java.io.InputStreamReader;  
public class syscmd extends Object{
        public static void runcmd(int bufSize, String command, String result[]){
       //String result;
       String returnResult;
       int i = 0;
       try {
           Process p;
           p = Runtime.getRuntime().exec(command);
           //BufferedInputStream bis=new BufferedInputStream(p.getInputStream(),bufSize);
           //byte buffer[]=new byte[bufSize];
           
           BufferedInputStream in = new BufferedInputStream(p.getInputStream());  
            BufferedReader inBr = new BufferedReader(new InputStreamReader(in));  
            String lineStr; 
           while ((lineStr = inBr.readLine()) != null) {
            //System.out.println(lineStr);
            if(lineStr.indexOf("內(nèi)存") > 0) {
              System.out.println(lineStr);
              #sql{insert into os_meminfo(info) values(:lineStr)};
            }
           }
          
           //while(bis.read(buffer,0,bufSize)!=-1);
              //System.out.write(buffer,0,len);
           p.waitFor();
          // for(int i= 0; i<=buffer.length; i++){
           //System.out.println(new String(buffer));
           //returnResult = new String(buffer);
           //#sql{insert into scott.meminfo3 values(:returnResult)};
           //returnResult = returnResult.substring(2000, 4000);
          // #sql{insert into scott.meminfo3 values(:returnResult)};
           //}
           inBr.close();
           in.close();
           
           
       }catch (Exception e){
           e.printStackTrace();
       }
    }
}
/
CREATE OR REPLACE procedure syscmd( bufsize in number, cmd in varchar2, result out varchar2) as
language java
name 'syscmd.runcmd(int, java.lang.String, java.lang.String[])';


CREATE OR REPLACE PROCEDURE sysinfo_test AS
  res VARCHAR2(256);
BEGIN
  syscmd(50000000, 'C:\Windows\System32\systeminfo.exe', res);
  dbms_output.put_line(res);
END;
/


調(diào)用:
begin
  delete from os_meminfo;
  sysinfo_test;
  commit;
end;


磁盤:

create table os_disk_usage(
  disk_name varchar2(200),
  free   number,
  total  number
)
/


create or replace and compile java source named mydiskusage as
import java.io.File;
import java.text.DecimalFormat;


public class MyDiskUsage {


  public static void main(String[] args) {


    File[] roots = File.listRoots();// 獲取磁盤分區(qū)列表
    for (File file : roots) {
      System.out.println(file.getPath() + "信息如下:");
      long free = file.getFreeSpace();
      long total = file.getTotalSpace();
      long use = total - free;
      System.out.println("空閑未使用 = " + change(free) + "G");// 空閑空間
      System.out.println("已經(jīng)使用 = " + change(use) + "G");// 可用空間
      System.out.println("總?cè)萘?= " + change(total) + "G");// 總空間
      System.out.println("使用百分比 = " + bfb(use, total));
      System.out.println();
    }
  }


  public static void getDiskUage() throws java.sql.SQLException{
  //System.out.println("HELLLLL00000000000"); 
    File[] roots = File.listRoots();// 獲取磁盤分區(qū)列表
     System.out.println(roots.length);
    for (File file : roots) {
      String diskName = file.getPath();
      long free = file.getFreeSpace();
      long total = file.getTotalSpace();
      long use = total - free;
      System.out.println(diskName + "total:" + total);
      #sql{insert into  h3.os_disk_usage( disk_name,free,total) values(:diskName,:free, :total)};
      
    }


  }


  public static long change(long num) {
    // return num;
    return num / 1024 / 1024 / 1024;
  }


  public static String bfb(Object num1, Object num2) {
    double val1 = Double.valueOf(num1.toString());
    double val2 = Double.valueOf(num2.toString());
    if (val2 == 0) {
      return "0.0%";
    } else {
      DecimalFormat df = new DecimalFormat("#0.00");
      return df.format(val1 / val2 * 100) + "%";
    }
  }


}
/
create or replace procedure mydiskusage  
    as 
    language java name 'MyDiskUsage.getDiskUage()';  


BEGIN
  DELETE FROM os_disk_usage;
  mydiskusage;
  COMMIT;
END;



以上過程均以sys用戶運(yùn)行。非sys用戶, 需要授權(quán)(dbms_java.grant_permission)

感謝各位的閱讀!關(guān)于“Windows如何使用java過程獲取操作系統(tǒng)磁盤以及內(nèi)存信息”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI