package pp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ReadTxt {
public static void main(String args[]) throws Exception{
File file=new File("d://B17LOG.txt");
File wfile = new File("d://out.txt");
BufferedReader reader = null;
BufferedWriter bw =new BufferedWriter(new FileWriter(wfile));
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
Date date=new Date();
long start=0;//begin time to long type
long end=0;//end time to long type
double s1=0;//start to double type
double e1=0;//end to double type
String para2[];
String startTime="";
String endTime="";
String sql="";
String para="";
String repsql="";// replace sql;
String befsql="";// before sql;
//日期格式
SimpleDateFormat sim=new SimpleDateFormat("HH:mm:ss,SSS");
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 出现Connection - {conn-100255} Connection 结尾
// System.out.println(tempString.lastIndexOf("Connection"));
//begin time
if(tempString.lastIndexOf("Connection")==57){
startTime=tempString.substring(11,tempString.indexOf("DEBUG")-1);
System.out.print(startTime+"-----");
date=sim.parse(startTime);
start=date.getTime();
s1=start;
}
//end time
if(tempString.indexOf("Returned connection")>0){
endTime=tempString.substring(11,tempString.indexOf("DEBUG")-1);
System.out.print(endTime+"-----");
date=sim.parse(endTime);
end=date.getTime();
e1=end;
}
//sql
if(tempString.indexOf("Executing Statement:")>0){
sql=tempString.substring(tempString.indexOf("Executing Statement:")+24);
befsql=sql;
//sql
}
//parameters
if(tempString.indexOf("Parameters:")>0){
para=tempString.substring(tempString.indexOf("Parameters:"));
String para1=tempString.substring(tempString.indexOf("Parameters:")+13);
//first parameter[]
para2=para1.replace("]", "").split(",");
/*for(int i=0;i<para2.length;i++){
System.out.println(para2[i]+"**");
}*/
//replace "?";
if(sql.length()>0&¶2[0]!=null){
//System.out.println(pp);
for(int i=0;i<para2.length;i++){
int local=sql.indexOf("?");
if(local>0){
String pp=sql.substring(0, local);
sql=pp+para2[i]+sql.substring(local+1);
}
}
//System.out.println(sql);// after repalce sql;
repsql=sql;
}
}
//end time-start time
if(start>0&&end>0){
double second=(e1-s1)/1000;//to second;
System.out.print(second+"s"+"\n");
System.out.println(befsql);
System.out.println(repsql);
System.out.println(para);
//写入文件
FileWriter writer = new FileWriter(wfile, true);
writer.write(startTime+"-----" +endTime+"----"+second+"s"+"\n"+befsql+"\n"+repsql+"\n"+para+"\n");
writer.close();
end=0;
start=0;
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
}
评论3
最新资源