Windows使用java过程获取操作系统磁盘以及内存信息

内存:先建立一个表, 然后调用操作系统命令获取内存容量等信息
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("内存") > 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;
/


调用:
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();// 获取磁盘分区列表
    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("已经使用 = " + change(use) + "G");// 可用空间
      System.out.println("总容量 = " + 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();// 获取磁盘分区列表
     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  h2.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用户运行。非sys用户, 需要授权(dbms_java.grant_permission)



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8520577/viewspace-2144553/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8520577/viewspace-2144553/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值