//写入磁盘
public static void writ(int sparseArr[][]) {
Systemoutprintln("写入磁盘的数据中~~~~~~");
File file = new File("E:\\java\\sparseArrtxt");
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
if (!fileexists()) {
filecreateNewFile();
}
StringBuilder allBuilder = new StringBuilder();
for (int[] rows : sparseArr) {
StringBuilder rowBuilder = new StringBuilder();
for (int item : rows) {
rowBuilderappend(item + "\t");
}
allBuilderappend(rowBuilder + "\n");
}
bwwrite(StringvalueOf(allBuilder));
bwflush();
bwclose();
} catch (IOException e) {
eprintStackTrace();
}
}
//读取磁盘
public static int[][] read() {
Systemoutprintln("读取磁盘的数据中~~~~~~");
File file = new File("E:\\java\\sparseArrtxt");
int[][] sparseArr = null;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
if (!fileexists()) {
filecreateNewFile();
}
String row = brreadLine();
String[] s = new String(row)split("\t");
sparseArr = new int[IntegerparseInt(s[0])][IntegerparseInt(s[1])];
while ((row = brreadLine()) != null) {
String[] s2 = new String(row)split("\t");
sparseArr[IntegerparseInt(s2[0])][IntegerparseInt(s2[1])] = IntegerparseInt(s2[2]);
}
brclose();
} catch (IOException e) {
eprintStackTrace();
}
return sparseArr;
}
使用sigar,
private static void file() throws Exception {
Sigar sigar = new Sigar();
FileSystem fslist[] = sigargetFileSystemList();
for (int i = 0; i < fslistlength; i++) {
Systemoutprintln("分区的盘符名称" + i);
FileSystem fs = fslist[i];
// 分区的盘符名称
Systemoutprintln("盘符名称: " + fsgetDevName());
// 分区的盘符名称
Systemoutprintln("盘符路径: " + fsgetDirName());
Systemoutprintln("盘符标志: " + fsgetFlags());//
// 文件系统类型,比如 FAT32、NTFS
Systemoutprintln("盘符类型: " + fsgetSysTypeName());
// 文件系统类型名,比如本地硬盘、光驱、网络文件系统等
Systemoutprintln("盘符类型名: " + fsgetTypeName());
// 文件系统类型
Systemoutprintln("盘符文件系统类型: " + fsgetType());
switch (fsgetType()) {
case 0: // TYPE_UNKNOWN :未知
break;
case 1: // TYPE_NONE
break;
case 2: // TYPE_LOCAL_DISK : 本地硬盘
// 文件系统总大小
FileSystemUsage usage = null;
usage = sigargetFileSystemUsage(fsgetDirName());
Systemoutprintln(fsgetDevName() + "总大小: " + usagegetTotal() + "KB");
// 文件系统剩余大小
Systemoutprintln(fsgetDevName() + "剩余大小: " + usagegetFree() + "KB");
// 文件系统可用大小
Systemoutprintln(fsgetDevName() + "可用大小: " + usagegetAvail() + "KB");
// 文件系统已经使用量
Systemoutprintln(fsgetDevName() + "已经使用量: " + usagegetUsed() + "KB");
double usePercent = usagegetUsePercent() 100D;
// 文件系统资源的利用率
Systemoutprintln(fsgetDevName() + "资源的利用率: " + usePercent + "%");
break;
case 3:// TYPE_NETWORK :网络
break;
case 4:// TYPE_RAM_DISK :闪存
break;
case 5:// TYPE_CDROM :光驱
break;
case 6:// TYPE_SWAP :页面交换
break;
}
}
return;
}
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中如何通过IO流将稀疏数组写入磁盘和从磁盘中读取,整行存,整行取全部的内容,包括:java中如何通过IO流将稀疏数组写入磁盘和从磁盘中读取,整行存,整行取、Java中如何获得某个磁盘(根目录)比如C盘、怎么用java代码读取linux主机的磁盘使用信息,同时截取出文件系统和已使用情况 放在map中可以得到keyvalu等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)