这个不是硬盘访问的问题,你把放到Web工程外,那么你不发布而直接访问时(就是直接将网页拖到浏览器中),那么此时用的是file://X:xxx/xx/testhtml的形式,而直接通过浏览器使用>
package comcmmbutil;
import javaio;
/
linux 下cpu 内存 磁盘 jvm的使用监控
@author avery_leo
/
public class DiskSpace {
/
获取cpu使用情况
@return
@throws Exception
/
public double getcpuUsage() throws Exception {
double cpuUsed = 0;
Runtime rt = RuntimegetRuntime();
Process p = rtexec("top -b -n 1");// 调用系统的“top"命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(pgetInputStream()));
String str = null;
String[] strArray = null;
while ((str = inreadLine()) != null) {
int m = 0;
if (strindexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&
strArray = strsplit(" ");
for (String tmp : strArray) {
if (tmptrim()length() == 0)
continue;
if (++m == 9) {// 第9列为cpu的使用百分比(RedHat
cpuUsed += DoubleparseDouble(tmp);
}
}
}
}
} catch (Exception e) {
eprintStackTrace();
} finally {
inclose();
}
return cpuUsed;
}
/
内存监控
@return
@throws Exception
/
public double getMemUsage() throws Exception {
double menUsed = 0;
Runtime rt = RuntimegetRuntime();
Process p = rtexec("top -b -n 1");// 调用系统的“top"命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(pgetInputStream()));
String str = null;
String[] strArray = null;
while ((str = inreadLine()) != null) {
int m = 0;
if (strindexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&
//
// Systemoutprintln("------------------3-----------------");
strArray = strsplit(" ");
for (String tmp : strArray) {
if (tmptrim()length() == 0)
continue;
if (++m == 10) {
// 9)--第10列为mem的使用百分比(RedHat 9)
menUsed += DoubleparseDouble(tmp);
}
}
}
}
} catch (Exception e) {
eprintStackTrace();
} finally {
inclose();
}
return menUsed;
}
/
获取磁盘空间大小
@return
@throws Exception
/
public double getDeskUsage() throws Exception {
double totalhd = 0;
double usedhd = 0;
Runtime rt = RuntimegetRuntime();
Process p = rtexec("df -hl /home");//df -hl 查看硬盘空间
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(pgetInputStream()));
String str = null;
String[] strArray = null;
while ((str = inreadLine()) != null) {
int m = 0;
strArray = strsplit(" ");
for (String tmp : strArray) {
if (tmptrim()length() == 0)
continue;
++m;
Systemoutprintln("----tmp----" + tmp);
if (tmpindexOf("G") != -1) {
if (m == 2) {
Systemoutprintln("---G----" + tmp);
if (!tmpequals("") && !tmpequals("0"))
totalhd += DoubleparseDouble(tmp
substring(0, tmplength() - 1)) 1024;
}
if (m == 3) {
Systemoutprintln("---G----" + tmp);
if (!tmpequals("none") && !tmpequals("0"))
usedhd += DoubleparseDouble(tmpsubstring(
0, tmplength() - 1)) 1024;
}
}
if (tmpindexOf("M") != -1) {
if (m == 2) {
Systemoutprintln("---M---" + tmp);
if (!tmpequals("") && !tmpequals("0"))
totalhd += DoubleparseDouble(tmp
substring(0, tmplength() - 1));
}
if (m == 3) {
Systemoutprintln("---M---" + tmp);
if (!tmpequals("none") && !tmpequals("0"))
usedhd += DoubleparseDouble(tmpsubstring(
0, tmplength() - 1));
Systemoutprintln("----3----" + usedhd);
}
}
}
}
} catch (Exception e) {
eprintStackTrace();
} finally {
inclose();
}
//上面写在userd和total写反了,懒得改了,就反着用了
Systemoutprintln("----totalhd----" + usedhd);
Systemoutprintln("----usedhd----" + totalhd);
return (totalhd / usedhd) 100;
}
public static void main(String[] args) throws Exception {
DiskSpace cpu = new DiskSpace();
Systemoutprintln("---------------cpu used:" + cpugetcpuUsage() + "%");
Systemoutprintln("---------------mem used:" + cpugetMemUsage() + "%");
Systemoutprintln("---------------HD used:" + cpugetDeskUsage() + "%");
Systemoutprintln("------------jvm监控----------------------");
Runtime lRuntime = RuntimegetRuntime();
Systemoutprintln("--------------Free Momery:" + lRuntimefreeMemory()+"K");
Systemoutprintln("--------------Max Momery:" + lRuntimemaxMemory()+"K");
Systemoutprintln("--------------Total Momery:" + lRuntimetotalMemory()+"K");
Systemoutprintln("---------------Available Processors :"
+ lRuntimeavailableProcessors());
}
}
java中可以使用file对象,获取当前电脑硬盘基本信息,示例如下:
import javaioFile;
public class DiskSpaceDetail {
public static void main(String[] args) {
File diskPartition = new File("C:");
long totalCapacity = diskPartitiongetTotalSpace();
long freePartitionSpace = diskPartitiongetFreeSpace();
long usablePatitionSpace = diskPartitiongetUsableSpace();
Systemoutprintln(" Sizes in Mega Bytes \n");
Systemoutprintln("Total C partition size : " + totalCapacity / (10241024) + " MB");
Systemoutprintln("Usable Space : " + usablePatitionSpace / (1024 1024) + " MB");
Systemoutprintln("Free Space : " + freePartitionSpace / (1024 1024) + " MB");
Systemoutprintln("\n Sizes in Giga Bytes \n");
Systemoutprintln("Total C partition size : " + totalCapacity / (102410241024) + " GB");
Systemoutprintln("Usable Space : " + usablePatitionSpace / (1024 10241024) + " GB");
Systemoutprintln("Free Space : " + freePartitionSpace / (1024 10241024) + " GB");
}
}
问了一下我远标出来的程序员哥们,她说,你看下对不对
swing下的包,javaxswingfilechooserFileSystemView;可以获取:
FileSystemView fileSys=FileSystemViewgetFileSystemView(); //获取当前系统文件类型
//获取系统的所有盘符或系统卷类型
for(File f:FilelistRoots()){
Systemoutprintln(fileSysgetSystemDisplayName(f));//获取系统卷标及名字
Systemoutprintln(fileSysgetSystemTypeDescription(f));//获取系统卷的类型
Systemoutprintln(fgetTotalSpace());//获取该卷大小(单位:字节)
Systemoutprintln(fgetFreeSpace());//获取该卷可用大小(单位:字节)
}
以上就是关于java获得当前系统内存及硬盘使用情况全部的内容,包括:java获得当前系统内存及硬盘使用情况、java如何获取远程计算机的系统信息,cpu使用情况,磁盘使用情况等等、java , 浏览器输入url地址,获取硬盘某个pdf文件。怎么做 如下图这种效果。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)