去读日志内容,过滤字段,重新生成文件

package service;

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

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import static service.readdemo.subString;

/**
 * @author Kari
 * @date 2021年07月07日下午7:44
 */
public class r_w_demo {
    /**
     * @Kari
     */
    public static void main(String[] args) {
        read("api.txt");
    }

    private static void read(String file) {
        MyCacheLock cacheLock = new MyCacheLock();
        // 创建字符流对象,并根据已创建的字节流对象创建字符流对象
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader raf = null;

        try {
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            raf = new BufferedReader(isr);

          String  s = null;

            // 读取文件内容,并将其打印
            while ((s = raf.readLine()) != null) {
                String result = subString(s, "{\"id\":");
                JSONObject jo = JSONObject.parseObject(result);
                String id =jo.getString("id");
                Integer price = (Integer) jo.getJSONArray("seat").getJSONObject(0).getJSONArray("ad").getJSONObject(0).get("bid_price");
                  new Thread(() -> {
                      cacheLock.put("requestId", String.valueOf(id),"price", String.valueOf(price));
                  }).start();
                  new Thread(() -> {
                      cacheLock.get("requestId");
                  }).start();
                  new Thread(() -> {
                      cacheLock.get("price");
                  }).start();
            }

        } catch (Exception e) {

            e.printStackTrace();
        } finally {

            try {
                raf.close();

            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

}

class MyCacheLock {
    private volatile Map<String, String> map = new HashMap<String, String>();
    //读写锁 更加细粒度读控制
    ReadWriteLock lock = new ReentrantReadWriteLock();


    public void put(String key, String value,String key2, String value2) {
        lock.writeLock().lock();
        Reader reader = null;
        Reader reader2 = null;
        BufferedWriter bw = null;
        System.out.println(Thread.currentThread().getName());
        try {
            map.put(key,value);
            map.put(key2,value2);
            //确定流
            //操作(分段读取)
            reader = new StringReader(map.get(key));
            reader2 = new StringReader(map.get(key2));
            char[] c = new char[1024 * 10];//缓存容器
            char[] c2 = new char[1024 * 10];//缓存容器
            int len = -1;//接受长度
            int len2 = -1;//接受长度
            while ((len = reader.read(c)) != -1 && (len2 = reader2.read(c2)) != -1) {
                String s = new String(c, 0, len);
                String s2 = new String(c2, 0, len2);
                bw = new BufferedWriter(new FileWriter("Tanx_resp.txt", true));// FileWriter(csv, true) 当参数为true的时候写入的数据会叠加不会覆盖
                bw.write("{\""+key+"\"" + ":\"" + s + "\",");
                bw.write("\""+key2+"\"" + ":\"" + s2 + "\"}");
                bw.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.writeLock().unlock();
            try {
                reader.close();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public String get(String key) {
            lock.readLock().lock();
            String a =map.get(key);
            lock.readLock().unlock();
            return a;
        }
}

可能会用到的字符串截取

package service;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * @author Kari
 * @date 2021年07月07日下午7:14
 */
public class readdemo {
    public static void main(String[] args) {
        String lineTxt = null;
        try {
            File file = new File("tanx_nginx202.txt");
            if (file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
                BufferedReader br = new BufferedReader(isr);
                int i=0;
                while ((lineTxt = br.readLine()) != null) {
                    String result = subString(lineTxt, "ex=");
                    i++;
                    if (i==5)break;
                    System.out.println(result);
                }
                br.close();
            } else {
                System.out.println("文件不存在!");
            }
        } catch (Exception e) {
            System.out.println("文件读取错误!");
        }
    }

    /**
     * 截取指定字段
     */
    public static String subString(String str, String strStart) {

        /* 找出指定的2个字符在 该字符串里面的 位置 */
        int strStartIndex = str.indexOf(strStart);

        /* index为负数 即表示该字符串中没有该字符 */
        if (strStartIndex < 0) {
            return "字符串 :" + str + "中不存在 " + strStart + ", 无法截取目标字符串";
        }
        /* 开始截取 */
        String result = str.substring(strStartIndex);
        return result;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值