日期:2014-05-20  浏览次数:20739 次

新手求助,非常非常感谢
实在是太着急了,所以请大家帮忙,我一定会努力学习java的,现在还不太熟悉java,所以请教下各位,我想从记事本里读内容,并比较load average的第一个参数,把最大的那个参数所在的那一段内容都打印出来,应该怎么写呀
top - 16:10:41 up 51 days, 5:03, 3 users, load average: 0.05, 0.63, 0.41
Tasks: 135 total, 1 running, 134 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.7%us, 0.1%sy, 0.0%ni, 99.0%id, 0.2%wa, 0.0%hi, 0.1%si, 0.0%st
Mem: 6106860k total, 1857816k used, 4249044k free, 109196k buffers
Swap: 5144568k total, 92k used, 5144476k free, 1039700k cached

  PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  
  1 root 15 0 10348 688 576 S 0.0 0.0 0:00.98 init  
 
   
top - 16:10:51 up 51 days, 5:03, 3 users, load average: 0.04, 0.61, 0.41
Tasks: 135 total, 1 running, 134 sleeping, 0 stopped, 0 zombie
Cpu(s): 2.1%us, 0.3%sy, 0.0%ni, 97.4%id, 0.0%wa, 0.0%hi, 0.1%si, 0.0%st
Mem: 6106860k total, 1858060k used, 4248800k free, 109208k buffers
Swap: 5144568k total, 92k used, 5144476k free, 1039780k cached

  PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  
 3890 root 15 0 81196 30m 6152 S 2.7 0.5 7:34.93 Xvnc  
   
  这两条比较之后的结果应该是
top - 16:10:41 up 51 days, 5:03, 3 users, load average: 0.05, 0.63, 0.41
Tasks: 135 total, 1 running, 134 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.7%us, 0.1%sy, 0.0%ni, 99.0%id, 0.2%wa, 0.0%hi, 0.1%si, 0.0%st
Mem: 6106860k total, 1857816k used, 4249044k free, 109196k buffers
Swap: 5144568k total, 92k used, 5144476k free, 1039700k cached

  PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  
  1 root 15 0 10348 688 576 S 0.0 0.0 0:00.98 init

------解决方案--------------------
package com.mytest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class TestAll {
public static void main(String[] args) throws Exception {
new TestAll().read();
}


public void read(){
String str = "";
try {
File file = new File("C:\\Documents and Settings\\admins\\桌面\\read.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "GBK");
BufferedReader in = new BufferedReader(isr);
String line; //读取一行
Double average = null; //记录最大的load average的值
boolean isBig = false; //当前读取段落的load average最大时为true;
while ((line = in.readLine()) != null) {
//System.out.println(line);

if(line.startsWith("top")){
String topLine = line.substring(line.lastIndexOf(":")+1);
Double average1 = Double.parseDouble(topLine.substring(0,topLine.indexOf(",")).trim());
if(average == null || average < average1){
isBig = true;
average = average1;
str = "";
}else{
isBig = false;
}
}
if(isBig)
str += line+System.getProperty("line.separator");
}
in.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
new TestAll().write(str);
}

public void write(String line){
try {
File file = new File("C:\\Documents and Settings\\admins\\桌面\\write.txt");
FileOutputStream fos = new FileOutputStream(file);
PrintStream p = new PrintStream(fos);
p.println(line);
p.close();
fos.close();