讀取本地日誌並解析FileRead

package utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class DataUtilsSingle {
    public JSONObject innerJSON;
    public JSONObject outerJSON;

    //讀取path路徑的txt文件
    public void txt2String(String path) {
        //StringBuilder爲拼接字符串,減少對象的創建
        StringBuilder track_info_outer = new StringBuilder();
        StringBuilder track_info_inner = new StringBuilder();

        //文件讀取必須得用try catch,文件按行讀取
        try {
            File file = new File(path);
            BufferedReader br = new BufferedReader(new FileReader(file));//構造一個BufferedReader類來讀取文件
            String s;
            boolean isInner = false;
            while ((s = br.readLine()) != null) {//使用readLine方法,一次讀一行
                String result = s.trim();//去空白,末尾或者開頭。去空格
                //內外層數據分離,用{表示track_info內層數據開始,用},表示track_info內層數據結束。有的埋點數據內層有"utparam":"{
                //                 *       \"yk_abtest\":\"592:1381\"
                //                 *     }",這種數據,不能以}作爲內層結束判斷的標識
                /**
                 * track_info={
                 *   "k":"名偵探柯南",
                 *   "object_num":3,
                 *   "aaid":"074d90da3c8602ec629104ee7a57e796",
                 *    "utparam":"{
                 *       \"yk_abtest\":\"592:1381\"
                 *     }",
                 *   "newArch":"1",
                 *   "object_title":"30-60分鐘",
                 *   "source_from":"home"
                 * },
                 * spm=a2h0c.8166622.PhoneSokuFilter.sfilter_3,
                 * scm=20140669.search.filter.filter_30-60分鐘,
                 * pid=64b6847e992c4c45
                 */

                if (result.contains("{")) {
                    isInner = true;
                } else if (result.contains("},")) {
                    isInner = false;
                    //"A=1,B=2,C=3..."
                    track_info_inner.append(result);
                }
                boolean con = result.contains("},");
                if (!con) {
                    if (isInner) {
                        track_info_inner.append(result);
                    } else {
                        track_info_outer.append(result);
                    }

                }

            }
            br.close();
            //track_info_inner中等號(=)第一次出現的位置
            int firstIndex = track_info_inner.indexOf("=");
            //截取inner字符串,json格式{}裏所有內容,從{開始截取,直到}的位置,最後一位爲","
            String innerStr = track_info_inner.substring(firstIndex + 1);
            if (innerStr.endsWith(",")) {
                innerStr = innerStr.substring(0, innerStr.length() - 1);
            }
            String[] outer = track_info_outer.toString().split(",");
            innerJSON = JSON.parseObject(innerStr);
            outerJSON = new JSONObject();
            for (int i = 0; i < outer.length; i++) {
                String[] array = outer[i].split("=");
                outerJSON.put(array[0].trim(), array[1].trim());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

待續。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章