您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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過程獲取操作系統磁盤以及內存信息”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。