这篇文章给大家分享的是有关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 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用户运行。非sys用户, 需要授权(dbms_java.grant_permission)
感谢各位的阅读!关于“Windows如何使用java过程获取操作系统磁盘以及内存信息”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!