流读取日志并做数据处理

最近项目中有一个找数据的任务,一个一个数据去比对太烦了...

就写了个读取日志并且处理日志的代码...

功能实现了.还有很多地方需要改的,优化,先简单的记录一下.

流读取日志并做数据处理

日志文件都放在了D:\\job下,代码如下,简单记录

package com.trs.idap.web.rest.controller.readFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/11/6.
 * 描述:读取配置文件信息
 * @author Young
 * @create 2018-11-06 17:25
 */
public class Test {
    /**
     * 获取到所有的文件列表
     *  A:循环所有以Capture开始的文件,读取每一条数据
     *      拿到split切割","的数组的length-4个数字,取到数据targetNum,
     *          0丢弃,其他运行
     *      B:循环所有的以FaceResult开始的文件,读取每一条数据;如果该数据的split[0] == targetNum,跳出循环;
     *  执行完循环后,没有跳出循环,记录该文件名称和targetNum值.
     * */

    public static List<File> readfile(String filepath){
        try {
            List<File> list  = new ArrayList<>();
            File file = new File(filepath);
            if (!file.isDirectory()) {
                System.out.println("文件");
                System.out.println("path=" + file.getPath());
                System.out.println("absolutepath=" + file.getAbsolutePath());
                System.out.println("name=" + file.getName());
                list.add(file);
            } else if (file.isDirectory()) {
                System.out.println("文件夹");
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        System.out.println("path=" + readfile.getPath());
                        System.out.println("absolutepath="+ readfile.getAbsolutePath());
                        System.out.println("name=" + readfile.getName());
                        list.add(readfile);
                    }
                }
            }
            return list;
        } catch (Exception e) {
            System.out.println("readfile()   Exception:" + e.getMessage());
        }
        return  null;
    }
    /**
     * @Author Young
     * @Description 将文件中的内容读取出一个String
     * @Date 19:59 2018/11/6
     * @Param  * @param null
     * @return
     **/
    public static String  readToString(String fileName) {
        String encoding = "UTF-8";
        File file = new File(fileName);
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return new String(filecontent, encoding);
        } catch (UnsupportedEncodingException e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            return null;
        }
    }
    public static void main(String[] args) {
        List<File> readfile = readfile("D:\\job");
        for (File file : readfile){
            if (file.getName().startsWith("FaceResult")){
                String strText = readToString(file.getAbsolutePath());
                String[] objs = strText.split("\r\n");
                //获得数据的pid
                for (String obj : objs) {
                    String pid = getPid(obj);
                    //根据pid查询所有的数据中的值,0不做处理
                    if ("0".equalsIgnoreCase(pid) ){
                        continue;
                    }
                    //根据pid去找数据
                    Boolean flag = getTarget(readfile,pid);
                    if (!flag){
                        System.err.println(file.getName()+"中没有的pid为"+pid+"的数据,没有在facecapture中找到...");
                        System.err.println("该条数据的值为:"+obj);
                    }
                }
            }
        }
    }
    /**
     * @Author Young
     * @Description 根据pid查找FaceResult中是否有匹配的数据
     * @Date 19:59 2018/11/6
     * @Param  * @param null
     * @return
     **/
    private static Boolean getTarget(List<File> files,String pid) {
        for (File file :files) {
            if (file.getName().startsWith("Capture")){
                String strs = readToString(file.getAbsolutePath());
                String[] objs = strs.split("\r\n");
                for (String str : objs){
                    String[] split = str.split(",");
                    String s = split[split.length-3];
                    if (pid.equals(s)){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * @Author Young
     * @Description 获得数据的倒数第三个值
     * @Date 19:26 2018/11/6
     * @Param  * @param null
     * @return
     **/
    private static String getPid(String obj) {
        String[] str = obj.split(",");
        return str[0];
    }
}