📄 regexstr.java
字号:
package cn.com.sihitech.cc.ivrlog;
import org.apache.log4j.Logger;
import java.util.regex.Pattern;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.Properties;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.io.File;
import java.io.FileOutputStream;
/**
*
* <p>Title: ivrLogf </p>
* <p>Description: find a special String in Log files</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author linus
* @version 2.0
*/
public class regexStr {
private static Logger logger = Logger.getLogger(regexStr.class);
private static String propertiesPath = "ApplicationResources.properties";
private static String dealNum = "";
private static String dealDate = "";
private static String dealStr = "";
private static String filename = "";
private static String IPPath[];
private static FileOutputStream fs = null;
private static boolean isNull = true;
public regexStr() {
}
/**
*读属性文件
* @return Properties
*/
public static Properties getProperties() throws Exception {
Properties p = null;
try {
InputStream in = new BufferedInputStream(new FileInputStream(
propertiesPath));
p = new Properties();
p.load(in);
}
catch (Exception e) {
throw new Exception("读属性文件出错", e);
}
return p;
}
/**
*从属性文件里读出filePath值
* @return String
*/
public static String readfilePath() throws Exception {
String filePath = "";
try {
Properties p = getProperties();
filePath = p.getProperty("filePath");
}
catch (Exception e) {
throw new Exception("读属性文件的filePath值出错", e);
}
return filePath;
}
/**
* 从属性文件里读出交易码dealStr值组装成正则表达式regexStr
* @return String
*/
public static String readregexStr() throws Exception {
String regexStr = "";
try {
Properties p = getProperties();
dealStr = p.getProperty("dealStr");
regexStr = "[\\u0000-\\uFFEF]{140}<message>[\\u0000-\\uFFEF&&[^g]]*" +
dealStr +
"[\\u0000-\\uFFEF&&[^g]]*</message>";
}
catch (Exception e) {
throw new Exception("读属性文件的要查找的字串dealStr值出错", e);
}
return regexStr;
}
/**
* 读入文件流转为字符串并查找相应的字串然后写到特定的文件里头
* @return String
*/
public static void readFile() throws Exception {
String src = readfilePath();
String fileRecord = "";
try {
Properties p = getProperties();
dealNum = p.getProperty("dealNum");
dealDate = p.getProperty("dealDate");
filename = dealNum + "_" + dealDate + ".log";
}
catch (Exception e) {
throw new Exception("读属性文件的交易码dealNum值或交易日期dealDate值出错", e);
}
try {
String srcAgr[] = src.split("\\|");
StringBuffer sb[] = new StringBuffer[srcAgr.length];
for (int j = 0; j < srcAgr.length; j++) {
sb[j] = new StringBuffer("");
}
FileReader file = null;
BufferedReader bufFile = null;
try {
IPPath = new String[srcAgr.length];
for (int i = 0; i < srcAgr.length; i++) {
if (srcAgr[i].indexOf("\\\\") != -1) {
String IP[] = srcAgr[i].split("\\\\");
IPPath[i] = IP[2];
}
else {
IPPath[i] = srcAgr[i];
}
File f = new File(srcAgr[i] + "\\" + filename);
if (!f.exists()) {
logger.error("在" + srcAgr[i] + "\\" + "目录下没有找到" + filename + "文件");
continue;
}
file = new FileReader(srcAgr[i] + "\\" + filename);
bufFile = new BufferedReader(file);
int j = 0;
while ( (fileRecord = bufFile.readLine()) != null) {
j++;
sb[i].append(fileRecord);
//98是log文件基本单元的行数
if (j % 9800 == 0) {
System.out.println("正在搜索第 " + (i + 1) + " 个服务器..............");
//readregexStr()从属性文件中获得正则表达式
//findstrfromFile()查找相应的字串
writeFile(findstrfromFile(readregexStr(), sb, i));
sb[i] = new StringBuffer("");
//System.out.println("搜索中..............");
}
}
//当行数小于98时,就不执行上面的写动作,留到最后来读取
writeFile(findstrfromFile(readregexStr(), sb, i));
sb[i] = new StringBuffer("");
System.out.println("搜索第 " + (i + 1) + " 个服务器结束!!!!");
}
}
finally {
if (bufFile != null) {
bufFile.close();
}
if (file != null) {
file.close();
}
}
}
catch (Exception e) {
e.printStackTrace();
throw new Exception("IOError. 读取日志文件出错:日志文件不存在,请到属性文件里查看filePath的值", e);
}
}
/**
* 从文件流转成的字符串中查找匹配的字符
* @param regexStr String
* @param fileStr String
*
*/
public static String findstrfromFile(String regexStr, StringBuffer fileStr[],
int i) throws
Exception {
StringBuffer buf = new StringBuffer("");
try {
Pattern p = Pattern.compile(regexStr);
Matcher m = p.matcher(fileStr[i].toString());
for (; m.find(); ) {
buf.append("IP地址 : " + IPPath[i] + "\n" + m.group());
}
if (!buf.toString().equals("")) {
logger.info(buf.toString());
}
}
catch (Exception e) {
throw new Exception("正则表达式匹配有错:请到属性文件里检查regexStr值是否正确", e);
}
return buf.toString();
}
/**
* 用于DOS窗口停止
*/
public static void stopDos() throws Exception {
String quitStr = "";
do {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
quitStr = br.readLine();
if (quitStr.equals("q")) {
break;
}
System.out.println("please input \"q\" to exit ");
}
while (! (quitStr.equals("q")));
}
/**
* 对文本进行格式化
* @param readStr String
* @return String
*/
public static String formatStr(String readStr) {
String regEx = "</field>"; //表示一个或多个a
Pattern pa = Pattern.compile(regEx);
Matcher m = pa.matcher(readStr);
readStr = m.replaceAll("</field>\n");
regEx = "<body>";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("<body>\n");
regEx = "<message>";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("<message>\n");
regEx = "<head>";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("<head>\n");
regEx = "</head>";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("</head>\n");
regEx = "<field-list name=\"ARRAY_010101\">";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("<field-list name=\"ARRAY_010101\">\n");
regEx = "<field-list name=\"0\">";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("<field-list name=\"0\">\n");
regEx = "</field-list>";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("</field-list>\n");
regEx = "</body>";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("</body>\n");
regEx = "</message>";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("</message>\n\n" +
"-------------------------------------------------\n\n");
regEx = "\\?>";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("?>\n");
regEx = "[-]+[-]+";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll("--------------------------------------------\n");
regEx = "\\s\\]";
pa = Pattern.compile(regEx);
m = pa.matcher(readStr);
readStr = m.replaceAll(" ]" + "\n");
return readStr;
}
/**
* 写入文件
* @param readStr String
* @throws Exception
*/
public static void writeFile(String readStr) throws Exception {
String Logpath = "";
if (!readStr.equals("")) {
isNull = false;
}
//对文本进行格式化
readStr = formatStr(readStr);
try {
Properties p = getProperties();
Logpath = p.getProperty("Logpath");
if (Logpath == null || Logpath.equals("")) {
Logpath = "d:\\IvrLog.txt";
}
if (fs == null) {
java.io.File myDelFile = new java.io.File(Logpath);
myDelFile.delete();
fs = new FileOutputStream(Logpath, true);
}
fs.write(readStr.getBytes("GBK"));
}
catch (Exception e) {
e.printStackTrace();
throw new Exception("写入" + Logpath + "文件出错", e);
}
}
/**
* 用设定的参数和软件打开你查找到的字串
* @param readStr String
* @throws Exception
*/
public static void openFile() throws Exception {
Properties p = getProperties();
String Logpath = p.getProperty("Logpath");
if (fs != null) {
fs.flush();
fs.close();
}
//isNull==true表示没有找到符合要求的字段
if (isNull == true) {
logger.error("没有在你所指定的文件里找到任何符合查找条件: " + dealStr + " 的字段");
System.out.println("查找结束!!!按'q'然后回车退出!!!");
}
else {
System.out.println("查找结束!!!按'q'然后回车退出!!!");
}
//执行dos命令打开日志文件
String autoOpen = p.getProperty("autoOpen");
String openEditor = p.getProperty("openEditor");
if ("1".equals(autoOpen) && isNull == false) {
Process pr;
try {
pr = Runtime.getRuntime().exec(openEditor + " " + Logpath);
//等待刚刚执行的命令的结束
while (true) {
if (pr.waitFor() == 0) {
break;
}
}
}
catch (Exception e) {
throw new Exception("打开" + Logpath + "文件出错", e);
}
}
}
/**
*主函数
* @param agrs String[]
*/
public static void main(String args[]) {
//属性文件
if (args[0] == null) {
propertiesPath =
"D:\\regexapp\\java\\classes\\ApplicationResources.properties";
}
else {
propertiesPath = args[0];
}
try {
System.out.println("开始查找..............");
//从日志文件中找出全部字符并查找符合指定正则表达式的字符串并写入文件
readFile();
//用指定编辑器打开保存有查找结果的文件
openFile();
//DOS窗口停止
stopDos();
}
catch (Exception ex2) {
ex2.printStackTrace();
logger.error(ex2);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -