本帖最后由 Henry_YQH 于 2013-05-31 19:35:06 编辑

解决方案 »

  1.   

    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 ;

    }

    }