利用Java文件类File的方法,获取磁盘文件的文件名、长度、大小等特性。

利用Java文件类File的方法,获取磁盘文件的文件名、长度、大小等特性。,第1张

package comtexst;

import javaioFile;

import javaioFileInputStream;

import javaioFileNotFoundException;

import javaioIOException;

public class GetFileMessageOfDisk {

/

@param args

/

public static void main(String[] args) {

File file = new File("D:/");

getMessage(file);

}

private static void getMessage(File file){

File[] files = filelistFiles();

if(files==null){

files = new File[0];

}

for(int i=0;i<fileslength;i++){

if(files[i]isDirectory()){

Systemoutprintln(files[i]getPath());

getMessage(files[i]);

}else{

try {

int length = new FileInputStream(files[i])available();

Systemoutprintln(files[i]getName()+" 长度:"+length+" 大小:"+length/1024+"KB");

} catch (FileNotFoundException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

}

}

}

}

}

1、在远程server里搭建一个>

String HDserialnumber=getHdSerialInfo() ;public static String getHdSerialInfo() { String line = ""; String HdSerial = "";//定义变量 硬盘序列号 try { Process proces = RuntimegetRuntime()exec("cmd /c dir c:");//获取命令行参数 BufferedReader buffreader = new BufferedReader( new InputStreamReader(procesgetInputStream())); while ((line = buffreaderreadLine()) != null) { if (lineindexOf("卷的序列号是 ") != -1) { //读取参数并获取硬盘序列号 HdSerial = linesubstring(lineindexOf("卷的序列号是 ") + "卷的序列号是 "length(), linelength()); break; // Systemoutprintln(HdSerial); } } } catch (IOException e) { // TODO Auto-generated catch block eprintStackTrace(); } return HdSerial;//返回硬盘序列号 卷的序列 非物理}

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的方法,获取磁盘文件的文件名、长度、大小等特性。全部的内容,包括:利用Java文件类File的方法,获取磁盘文件的文件名、长度、大小等特性。、java如何获取远程计算机的系统信息,cpu使用情况,磁盘使用情况等等、用Java怎么获取乌班图服务器的磁盘序列号等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9291910.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存