如何解析一个txt文件,获取有用字段.
本帖最后由 wallisnow 于 2013-01-20 23:07:26 编辑 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 3536 1288 ? Ss 08:06 0:00 /sbin/init
root 2 0.0 0.0 0 0 ? S 08:06 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 08:06 0:05 [ksoftirqd/0]
root 6 0.0 0.0 0 0 ? S 08:06 0:01 [migration/0]
root 7 0.0 0.0 0 0 ? S 08:06 0:00 [watchdog/0]
root 8 0.0 0.0 0 0 ? S 08:06 0:00 [migration/1]
root 10 0.0 0.0 0 0 ? S 08:06 0:05 [ksoftirqd/1]
root 12 0.0 0.0 0 0 ? S 08:06 0:00 [watchdog/1]
root 13 0.0 0.0 0 0 ? S< 08:06 0:00 [cpuset]
root 14 0.0 0.0 0 0 ? S< 08:06 0:00 [khelper]
root 15 0.0 0.0 0 0 ? S 08:06 0:00 [kdevtmpfs]
root 16 0.0 0.0 0 0 ? S< 08:06 0:00 [netns]
root 18 0.0 0.0 0 0 ? S 08:06 0:00 [sync_supers]
root 19 0.0 0.0 0 0 ? S 08:06 0:00 [bdi-default]
root 20 0.0 0.0 0 0 ? S< 08:06 0:00 [kintegrityd]
john 21788 1.9 1.1 110624 24240 ? Sl 15:25 0:04 gedit /home/john/ps.xml
我现在可以得到这样一个文件,可以是TXT等类型,(linux ps-aux > ps.txt).现在我需要的只是PID,START,TIME,COMMAND.以用来绘图表示一些指定的命令的运行。比如最后一行,我指出
gedit ----------------||||||||-------------
----表示没有执行,||||||表示执行过程,到结束,监视他这个过程。
现在我想用JAVA 执行terminal,ps -aux来得到当前进程结果,然后写入一个文件(暂时是txt,我也不知到什么好用),然后通过读取此文件,开始不断的找PID,START,COMMAND,执行此系列操作,(当然这样就不是实时得了,比如1秒一次),然后看进程pid是否还存在,如果不存在表示此进程已经关闭,如果程序依然在,描一个点在比如说一个panel上,或者画的花儿什么的,哈哈。
比如我,现在读到进程(21788),他是gedit,命令打开一个文件,我现在就在我的可视化界面上列出
----------|
一秒后刷新,看21788是否还在如果在
..........||
得了,大概就这么个意思,当然后面的都是后话,现在先要读者个文件按思路获得这些有用的值可能是当务之急,希望大家能给我帮助,尽量详细,有必要的话写一些代码吧。另外,如果我要是想实时的话是不是会比较困难呢,实际这里我是需要比如A计算机监视B计算机的。
十分感谢大家
[解决办法]
如果没有猜错,这应该是linux下的top命令的结果吧。
import java.io.*;这个例子你参考下。
import java.math.BigDecimal;
public class LinuxInformation {
private static double totalHD = 0;
public double getCpuUsage() throws Exception {
double cpuUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(" R ") != -1) {
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 9) {
cpuUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuUsed;
}
public double getMemUsage() throws Exception {
double menUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(" R ") != -1) {
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 10) {
menUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return menUsed;
}
public double getDeskUsage() throws Exception {
double usedHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
++m;
if (tmp.indexOf("G") != -1) {
if (m == 2) {
if (!tmp.equals("") && !tmp.equals("0"))
totalHD += Double.parseDouble(tmp
.substring(0, tmp.length() - 1)) * 1024;
}
if (m == 3) {
if (!tmp.equals("none") && !tmp.equals("0"))
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() - 1)) * 1024;
}
}
if (tmp.indexOf("M") != -1) {
if (m == 2) {
if (!tmp.equals("") && !tmp.equals("0"))
totalHD += Double.parseDouble(tmp
.substring(0, tmp.length() - 1));
}
if (m == 3) {
if (!tmp.equals("none") && !tmp.equals("0"))
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() - 1));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return (usedHD / totalHD) * 100;
}
public static double getHardDisk() {
return totalHD;
}
public static String getOsType()throws Exception
{
String osName = System.getProperty("os.name");
String osversion = System.getProperty("os.version");
return osName+":"+osversion;
}
public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
double value = (b.divide(one, scale, BigDecimal.ROUND_HALF_UP))
.doubleValue();
return value;
}
public static Double calCpuUsage1() throws Exception {
Double cpuUsed = 0.0;
Double idleUsed = 0.0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");
BufferedReader in = null;
{
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
int linecount = 0;
while ((str = in.readLine()) != null) {
linecount++;
if (linecount == 3) {
String[] s = str.split("%");
String idlestr = s[3];
String idlestr1[] = idlestr.split(" ");
idleUsed = Double.parseDouble(idlestr1[idlestr1.length - 1]);
cpuUsed = 100 - idleUsed;
System.out.println(cpuUsed);
break;
}
}
}
return round(cpuUsed/100,4);
}
public static void main(String[] args) throws Exception {
LinuxInformation cpu = new LinuxInformation();
System.out.println("ostype:" + calCpuUsage1());
}
}