求人帮忙解析一下下面这段日志,用java实现?? 本帖最后由 Henry_YQH 于 2013-05-31 19:35:06 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;public class ParseFile { FileInputStream fi = null; BufferedReader br = null; String newFilePath = ""; File newFile = null; FileWriter fw = null; BufferedWriter bw = null; public ParseFile() throws Exception { fi = new FileInputStream("D:\\file\\file.log"); br = new BufferedReader(new InputStreamReader(fi,"GB2312")); newFilePath = "D:\\file\\newFile.log"; newFile = new File(newFilePath); fw = new FileWriter(newFile, true); bw = new BufferedWriter(fw); } public static void main(String[] args) { try { ParseFile parseFile = new ParseFile(); parseFile.parseFile(); } catch (Exception e) { e.printStackTrace(); } } public void parseFile() throws Exception { Map<String,String> map = new HashMap<String, String>(); Map<String,String> tempMap = new HashMap<String,String>(); String tempString = ""; try{ tempString = br.readLine(); map.put("time",tempString.trim()); while(true){ readNewRegion(map , tempMap); } }catch (Exception e){ e.printStackTrace(); } if (bw != null) { bw.close(); } } public void readNewRegion(Map map ,Map tempMap)throws Exception{ if (tempMap.get("time")!=null){ map.put("time", tempMap.get("time")); tempMap.clear(); } String line = ""; String tempString = ""; if( (tempString = br.readLine())!= null){ String [] temp = tempString.split(":"); map.put("type", temp[0]); } String [] tempArr = new String [5]; String temp = ""; while((temp = br.readLine())!=null){ tempArr = temp.split(":"); if((tempArr.length == 3)&(tempArr[0].length()==2)){ tempMap.put("time", temp.trim()); break; } readNewLine(temp.trim(),map); } /*line =(map.get("time")) + "\t" + map.get("type") + "\t" + map.get("IP") + "\t" + map.get("MobileCode") + "\t" + map.get("MobileType") + "\t" + map.get("StockCode") + "\t" + map.get("CommBatchEntrustInfo") + "\t" + map.get("WTAccount") + "\t" + map.get("Price") + "\t" + map.get("Volume") + "\t" + map.get("Action") + "\t" + map.get("YybCode") + "\t" + map.get("Account") + "\t" + map.get("AccountType") + "\t" + map.get("Token") + "\t" + map.get("Reqno") + "\t" + map.get("MaxCount") + "\t" + map.get("ErrorNo") + "\t" + map.get("StockIndex") + "\t" + map.get("AccountIndex") + "\t"+ map.get("StockNumIndex")+ "\t" + map.get("ContactIndex")+ "\t"+ map.get("DrawIndex")+ "\t"+ map.get("UserName")+ "\t"+ map.get("UserType")+ "\t"+ map.get("GateWayIp")+ "\t" + map.get("Title")+ "\t"+ map.get("CellIndex")+ "\t"+ map.get("NeedCheck")+ "\t"+ map.get("ZLib")+ "\t"+ map.get("HsString"); map.clear();*/ line =(map.get("time")==null?"":map.get("time")) + "\t" + (map.get("type")==null?"":map.get("type"))+ "\t" + (map.get("IP")==null?"":map.get("IP")) + "\t" + (map.get("MobileCode")==null?"":map.get("MobileCode")) + "\t" + (map.get("MobileType")==null?"":map.get("MobileType")) + "\t" + (map.get("StockCode")==null?"":map.get("StockCode")) + "\t" + (map.get("CommBatchEntrustInfo")==null?"":map.get("CommBatchEntrustInfo")) + "\t" + (map.get("WTAccount")==null?"":map.get("WTAccount"))+ "\t" +(map.get("Price")==null?"":map.get("Price")) + "\t" +(map.get("Volume")==null?"":map.get("Volume")) + "\t" +(map.get("Action")==null?"":map.get("Action")) + "\t" + (map.get("YybCode")==null?"":map.get("YybCode")) + "\t" + (map.get("Account")==null?"":map.get("Account")) + "\t" + (map.get("AccountType")==null?"":map.get("AccountType")) + "\t" + (map.get("Token")==null?"":map.get("Token")) + "\t" +(map.get("Reqno")==null?"":map.get("Reqno")) + "\t" + (map.get("MaxCount")==null?"":map.get("MaxCount"))+ "\t" + (map.get("ErrorNo")==null?"":map.get("ErrorNo")) + "\t" +(map.get("StockIndex") ==null?"":map.get("StockIndex"))+ "\t" +( map.get("AccountIndex")==null?"":map.get("AccountIndex")) + "\t"+ (map.get("StockNumIndex")==null?"":map.get("StockNumIndex"))+ "\t" + (map.get("ContactIndex")==null?"":map.get("ContactIndex"))+ "\t"+ (map.get("DrawIndex")==null?"":map.get("DrawIndex"))+ "\t"+( map.get("UserName")==null?"":map.get("UserName"))+ "\t"+( map.get("UserType")==null?"":map.get("UserType"))+ "\t"+ (map.get("GateWayIp")==null?"":map.get("GateWayIp"))+ "\t" + (map.get("Title")==null?"":map.get("Title"))+ "\t"+ (map.get("CellIndex")==null?"":map.get("CellIndex"))+ "\t"+ (map.get("NeedCheck")==null?"":map.get("NeedCheck"))+ "\t"+ (map.get("ZLib")==null?"":map.get("ZLib"))+ "\t"+ (map.get("HsString")==null?"":map.get("HsString")); map.clear(); bw.write(line); bw.newLine(); System.out.println(line); bw.flush(); } public Map readNewLine(String line,Map map)throws Exception{ String [] temp =new String[2]; temp = line.split("="); if(temp.length > 1){ if(temp[0].equals("IP")){ map.put("IP", temp[1]); } if(temp[0].equalsIgnoreCase("MobileCode")){ map.put("MobileCode", temp[1]); } if(temp[0].equalsIgnoreCase("MobileType")){ map.put("MobileType", temp[1]); } if(temp[0].equalsIgnoreCase("StockCode")){ map.put("StockCode", temp[1]); } if(temp[0].equalsIgnoreCase("CommBatchEntrustInfo")){ map.put("CommBatchEntrustInfo", temp[1]); } if(temp[0].equalsIgnoreCase("WTAccount")){ map.put("WTAccount", temp[1]); } if(temp[0].equalsIgnoreCase("Price")){ map.put("Price", temp[1]); } if(temp[0].equalsIgnoreCase("Volume")){ map.put("Volume", temp[1]); } if(temp[0].equalsIgnoreCase("Action")){ map.put("Action", temp[1]); } if(temp[0].equalsIgnoreCase("YybCode")){ map.put("YybCode", temp[1]); } if(temp[0].equalsIgnoreCase("Account")){ map.put("Account", temp[1]); } if(temp[0].equalsIgnoreCase("AccountType")){ map.put("AccountType", temp[1]); } if(temp[0].equalsIgnoreCase("Token")){ map.put("Token", temp[1]); } if(temp[0].equalsIgnoreCase("Reqno")){ map.put("Reqno", temp[1]); } if(temp[0].equalsIgnoreCase("MaxCount")){ map.put("MaxCount", temp[1]); } if(temp[0].equalsIgnoreCase("ErrorNo")){ map.put("ErrorNo", temp[1]); } if(temp[0].equalsIgnoreCase("StockIndex")){ map.put("StockIndex", temp[1]); } if(temp[0].equalsIgnoreCase("AccountIndex")){ map.put("AccountIndex", temp[1]); } if(temp[0].equalsIgnoreCase("StockNumIndex")){ map.put("StockNumIndex", temp[1]); } if(temp[0].equalsIgnoreCase("ContactIndex")){ map.put("ContactIndex", temp[1]); } if(temp[0].equalsIgnoreCase("DrawIndex")){ map.put("DrawIndex", temp[1]); } if(temp[0].equalsIgnoreCase("UserName")){ map.put("UserName", temp[1]); } if(temp[0].equalsIgnoreCase("UserType")){ map.put("UserType", temp[1]); } if(temp[0].equalsIgnoreCase("GateWayIp")){ map.put("GateWayIp", temp[1]); } if(temp[0].equalsIgnoreCase("Title")){ map.put("Title", temp[1]); } if(temp[0].equalsIgnoreCase("CellIndex")){ map.put("CellIndex", temp[1]); } if(temp[0].equalsIgnoreCase("NeedCheck")){ map.put("NeedCheck", temp[1]); } if(temp[0].equalsIgnoreCase("ZLib")){ map.put("ZLib", temp[1]); } if(temp[0].equalsIgnoreCase("HsString")){ map.put("HsString", temp[1]); } } return map ; } } javascript中遍历List集合对象 Stateful bean was removed or timed out. java.代码生成器来了....开启节约编程时间的时代来了..... Struts中关于Html:select标签的一个问题 用jdom的xpath查询xml,怎样按某个字段进行排序,输出查询结果 一个关于设计的问题 JBuilder2005和WebLogic的问题。 hibernate连接 ms sql jdbc驱动? 菜鸟~~在线求助(快呀) 如何配置j2ee? 新人,求解释!! java如何将数据库表中某一列求和的结果集显示在界面上?
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;public class ParseFile { FileInputStream fi = null;
BufferedReader br = null;
String newFilePath = "";
File newFile = null;
FileWriter fw = null;
BufferedWriter bw = null;
public ParseFile() throws Exception {
fi = new FileInputStream("D:\\file\\file.log");
br = new BufferedReader(new InputStreamReader(fi,"GB2312"));
newFilePath = "D:\\file\\newFile.log";
newFile = new File(newFilePath);
fw = new FileWriter(newFile, true);
bw = new BufferedWriter(fw);
}
public static void main(String[] args) {
try {
ParseFile parseFile = new ParseFile();
parseFile.parseFile();
} catch (Exception e) {
e.printStackTrace();
}
}
public void parseFile() throws Exception { Map<String,String> map = new HashMap<String, String>();
Map<String,String> tempMap = new HashMap<String,String>();
String tempString = "";
try{
tempString = br.readLine();
map.put("time",tempString.trim());
while(true){
readNewRegion(map , tempMap);
}
}catch (Exception e){
e.printStackTrace();
}
if (bw != null) {
bw.close();
}
}
public void readNewRegion(Map map ,Map tempMap)throws Exception{
if (tempMap.get("time")!=null){
map.put("time", tempMap.get("time"));
tempMap.clear();
}
String line = "";
String tempString = "";
if( (tempString = br.readLine())!= null){
String [] temp = tempString.split(":");
map.put("type", temp[0]);
}
String [] tempArr = new String [5];
String temp = "";
while((temp = br.readLine())!=null){
tempArr = temp.split(":");
if((tempArr.length == 3)&(tempArr[0].length()==2)){
tempMap.put("time", temp.trim());
break;
}
readNewLine(temp.trim(),map);
}
/*line =(map.get("time")) + "\t" + map.get("type") + "\t" + map.get("IP") + "\t" + map.get("MobileCode") + "\t" + map.get("MobileType") + "\t"
+ map.get("StockCode") + "\t" + map.get("CommBatchEntrustInfo") + "\t" + map.get("WTAccount") + "\t" + map.get("Price") + "\t"
+ map.get("Volume") + "\t" + map.get("Action") + "\t" + map.get("YybCode") + "\t" + map.get("Account") + "\t"
+ map.get("AccountType") + "\t" + map.get("Token") + "\t" + map.get("Reqno") + "\t" + map.get("MaxCount")
+ "\t" + map.get("ErrorNo") + "\t" + map.get("StockIndex") + "\t" + map.get("AccountIndex") + "\t"+ map.get("StockNumIndex")+ "\t"
+ map.get("ContactIndex")+ "\t"+ map.get("DrawIndex")+ "\t"+ map.get("UserName")+ "\t"+ map.get("UserType")+ "\t"+ map.get("GateWayIp")+ "\t"
+ map.get("Title")+ "\t"+ map.get("CellIndex")+ "\t"+ map.get("NeedCheck")+ "\t"+ map.get("ZLib")+ "\t"+ map.get("HsString");
map.clear();*/
line =(map.get("time")==null?"":map.get("time")) + "\t" + (map.get("type")==null?"":map.get("type"))+ "\t" + (map.get("IP")==null?"":map.get("IP")) + "\t" + (map.get("MobileCode")==null?"":map.get("MobileCode")) + "\t" + (map.get("MobileType")==null?"":map.get("MobileType")) + "\t"
+ (map.get("StockCode")==null?"":map.get("StockCode")) + "\t" + (map.get("CommBatchEntrustInfo")==null?"":map.get("CommBatchEntrustInfo")) + "\t" + (map.get("WTAccount")==null?"":map.get("WTAccount"))+ "\t" +(map.get("Price")==null?"":map.get("Price")) + "\t"
+(map.get("Volume")==null?"":map.get("Volume")) + "\t" +(map.get("Action")==null?"":map.get("Action")) + "\t" + (map.get("YybCode")==null?"":map.get("YybCode")) + "\t" + (map.get("Account")==null?"":map.get("Account")) + "\t"
+ (map.get("AccountType")==null?"":map.get("AccountType")) + "\t" + (map.get("Token")==null?"":map.get("Token")) + "\t" +(map.get("Reqno")==null?"":map.get("Reqno")) + "\t" + (map.get("MaxCount")==null?"":map.get("MaxCount"))+ "\t"
+ (map.get("ErrorNo")==null?"":map.get("ErrorNo")) + "\t" +(map.get("StockIndex") ==null?"":map.get("StockIndex"))+ "\t" +( map.get("AccountIndex")==null?"":map.get("AccountIndex")) + "\t"+ (map.get("StockNumIndex")==null?"":map.get("StockNumIndex"))+ "\t"
+ (map.get("ContactIndex")==null?"":map.get("ContactIndex"))+ "\t"+ (map.get("DrawIndex")==null?"":map.get("DrawIndex"))+ "\t"+( map.get("UserName")==null?"":map.get("UserName"))+ "\t"+( map.get("UserType")==null?"":map.get("UserType"))+ "\t"+ (map.get("GateWayIp")==null?"":map.get("GateWayIp"))+ "\t"
+ (map.get("Title")==null?"":map.get("Title"))+ "\t"+ (map.get("CellIndex")==null?"":map.get("CellIndex"))+ "\t"+ (map.get("NeedCheck")==null?"":map.get("NeedCheck"))+ "\t"+ (map.get("ZLib")==null?"":map.get("ZLib"))+ "\t"+ (map.get("HsString")==null?"":map.get("HsString")); map.clear();
bw.write(line);
bw.newLine();
System.out.println(line);
bw.flush();
}
public Map readNewLine(String line,Map map)throws Exception{
String [] temp =new String[2];
temp = line.split("=");
if(temp.length > 1){
if(temp[0].equals("IP")){
map.put("IP", temp[1]);
}
if(temp[0].equalsIgnoreCase("MobileCode")){
map.put("MobileCode", temp[1]);
}
if(temp[0].equalsIgnoreCase("MobileType")){
map.put("MobileType", temp[1]);
}
if(temp[0].equalsIgnoreCase("StockCode")){
map.put("StockCode", temp[1]);
}
if(temp[0].equalsIgnoreCase("CommBatchEntrustInfo")){
map.put("CommBatchEntrustInfo", temp[1]);
}
if(temp[0].equalsIgnoreCase("WTAccount")){
map.put("WTAccount", temp[1]);
}
if(temp[0].equalsIgnoreCase("Price")){
map.put("Price", temp[1]);
}
if(temp[0].equalsIgnoreCase("Volume")){
map.put("Volume", temp[1]);
}
if(temp[0].equalsIgnoreCase("Action")){
map.put("Action", temp[1]);
}
if(temp[0].equalsIgnoreCase("YybCode")){
map.put("YybCode", temp[1]);
}
if(temp[0].equalsIgnoreCase("Account")){
map.put("Account", temp[1]);
}
if(temp[0].equalsIgnoreCase("AccountType")){
map.put("AccountType", temp[1]);
}
if(temp[0].equalsIgnoreCase("Token")){
map.put("Token", temp[1]);
}
if(temp[0].equalsIgnoreCase("Reqno")){
map.put("Reqno", temp[1]);
}
if(temp[0].equalsIgnoreCase("MaxCount")){
map.put("MaxCount", temp[1]);
}
if(temp[0].equalsIgnoreCase("ErrorNo")){
map.put("ErrorNo", temp[1]);
}
if(temp[0].equalsIgnoreCase("StockIndex")){
map.put("StockIndex", temp[1]);
}
if(temp[0].equalsIgnoreCase("AccountIndex")){
map.put("AccountIndex", temp[1]);
}
if(temp[0].equalsIgnoreCase("StockNumIndex")){
map.put("StockNumIndex", temp[1]);
}
if(temp[0].equalsIgnoreCase("ContactIndex")){
map.put("ContactIndex", temp[1]);
}
if(temp[0].equalsIgnoreCase("DrawIndex")){
map.put("DrawIndex", temp[1]);
}
if(temp[0].equalsIgnoreCase("UserName")){
map.put("UserName", temp[1]);
}
if(temp[0].equalsIgnoreCase("UserType")){
map.put("UserType", temp[1]);
}
if(temp[0].equalsIgnoreCase("GateWayIp")){
map.put("GateWayIp", temp[1]);
}
if(temp[0].equalsIgnoreCase("Title")){
map.put("Title", temp[1]);
}
if(temp[0].equalsIgnoreCase("CellIndex")){
map.put("CellIndex", temp[1]);
}
if(temp[0].equalsIgnoreCase("NeedCheck")){
map.put("NeedCheck", temp[1]);
}
if(temp[0].equalsIgnoreCase("ZLib")){
map.put("ZLib", temp[1]);
}
if(temp[0].equalsIgnoreCase("HsString")){
map.put("HsString", temp[1]);
}
}
return map ;
}
}