📄 processcount.java
字号:
/**
*
*/
package logfileAnalyse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Collections;
/**
* @author Jack
*
*/
public class ProcessCount extends EventCount {
private static final int PRINT_MAX_LENGTH = 60;
private String ProcessEvent;
private Map<String, Event> process;
public int eToPoffset;
/**
*
*/
public ProcessCount() {
/*
* Initial the offset from the field of Event to the field of Process name
*/
eToPoffset = 7;
ProcessEvent = "592";
process = new TreeMap<String, Event>();
}
/**
* Accept filename to be analysed
*
* @param fileIn
* filename to be analysed
*/
public ProcessCount(String filename) {
this();
fileIn = new File(filename);
if (!fileIn.exists()) {
System.err.println(filename + " doses not exits.");
System.exit(1);
}
fileNum++;
}
/*
* Processes the data(from the log file ) one line at a time, tokenizes the
* events and stores the process in a TreeMap with the process as the key
* and a process number as the value.
*/
public void calculate() {
BufferedReader reader = null;
String line = null;
StringTokenizer st = null;
try{
reader = new BufferedReader(new FileReader(fileIn));
line = reader.readLine();
while(line != null) {
st = new StringTokenizer(line, DELIMITER);
while(st.hasMoreTokens()) {
String token = st.nextToken();
if( isProcess(token) ) {
for(int i = 0; i < eToPoffset; i++) {
if(!st.hasMoreTokens())
break;
token = st.nextToken();
}
token = getProcessName(token);
Event value = process.get(token);
if(value == null)
process.put(token, new Event(token));
else {
value.increment();
process.put(token, value);
}
}
}
line = reader.readLine();
}
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
/*
* input string is process or not
*/
public boolean isProcess(String input) {
if(input.equals(ProcessEvent))
return true;
else
return false;
}
/*
* obtain the process directory and name from the input string
*
* @param str
* input string
*/
public String getProcessName(String str) {
String strPrefix = "Image File Name: ";
String st;
st = str.substring(strPrefix.length());
return st;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuffer strbf = new StringBuffer();
String sEvt = "Process Name";
String sFrq = "Frequency";
String s1 = " ";
String s2 = "-";
List<Event> entries = new ArrayList<Event>();
entries.addAll(process.values());
Collections.sort(entries, new Event());
strbf.append(sEvt);
for(int i = 0; i < (PRINT_MAX_LENGTH - sEvt.length() - sFrq.length()); i++)
strbf.append(s1);
strbf.append(sFrq).append(NEWLINE);
for(int i = 0; i < PRINT_MAX_LENGTH; i++)
strbf.append(s2);
strbf.append(NEWLINE);
for(Event evt : entries) {
evt.PRINT_MAX_LENGTH = PRINT_MAX_LENGTH;
strbf.append(evt).append(NEWLINE);
}
for(int i = 0; i < PRINT_MAX_LENGTH; i++)
strbf.append(s2);
strbf.append(NEWLINE);
return new String(strbf);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -