一个日志log分析的面试题

花了点时间做了一个面试题,记一下,追求快有点粗糙。

第一条是文件目录及病种名称,第4行是检测返回的结果,第一个结果如果不是跟目录病种对应的话, 那就是不正确的

 

 

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;

public class Main {
    public static void main(String[] args) throws IOException {
        getLog();
    }

    public static void getLog() {
        String fileName = "D:/filename.txt";
        readFileByRandomAccess(fileName);
    }

    public static void readFileByRandomAccess(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            // System.out.println("以行为单位读取文件内容,一次读一整行:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            int tur = 0;
            // 路径病种元素
            int cc = 0;
            List<String> ccList = new ArrayList<>();
            List<String> ccList1 = new ArrayList<>();
            // 结果元素
            int fh = 0;
            List<String> fhList = new ArrayList<>();
            List<String> fhList1 = new ArrayList<>();

            // 路径农作物品种
            List<String> cropVariety = new ArrayList<>();
            List<String> cropVariety1 = new ArrayList<>();
            // 病毒品种
            List<String> virusVariety = new ArrayList<>();

            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                // System.out.println("line " + line + ": " + tempString);
                line++;

                // 判断
                if (tempString.indexOf("C:") != -1) {
                    cc++;
                    ccList.add(tempString);
                    // 截取文件字符串
                    String[] strs = tempString.split("\\\\");
                    for (int i = 0, len = strs.length; i < len; i++) {
                        // System.out.println(strs[i].toString());
                        if (i == 6) {
                            ccList1.add(strs[i]);
                        }
                    }

                }
                if (tempString.indexOf("{\"") != -1) {
                    fh++;
                    fhList.add(tempString);
                    // 截取结果字符串
                    String[] strs1 = tempString.split("\"");
                    for (int i = 0, len = strs1.length; i < len; i++) {
                        // System.out.println(strs[i].toString());
                        if (i == 1) {
                            fhList1.add(strs1[i]);
                        }
                    }
                }

                // 判断农作物物种
                if (tempString.indexOf("C:") != -1) {
                    // 截取字符串
                    String[] strs = tempString.split("\\\\");
                    for (int i = 0, len = strs.length; i < len; i++) {
                        // System.out.println(strs[i].toString());
                        if (i == 5) {
                            cropVariety.add(strs[i]);
                        }
                    }
                }

            }

            /*
             * for(String a : ccList) { System.out.println(a); }
             */
            // 统计浓物品种
            for (int i = 0; i < cropVariety.size() - 1; i++) {
                if (i == 0) {
                    cropVariety1.add(cropVariety.get(i));
                    // System.out.println(cropVariety.get(i));
                } else if (cropVariety.get(i).equals(cropVariety.get(i + 1)) == false) {
                    cropVariety1.add(cropVariety.get(i + 1));
                    // System.out.println(cropVariety.get(i+1));
                }
            }
            // 统计病毒品种
            for (int i = 0; i < ccList1.size() - 1; i++) {
                if (i == 0) {
                    virusVariety.add(ccList1.get(i));
                    // System.out.println(ccList1.get(i));
                } else if (ccList1.get(i).equals(ccList1.get(i + 1)) == false) {
                    virusVariety.add(ccList1.get(i + 1));
                    // System.out.println(ccList1.get(i+1));
                }
            }

            // 开始循环输出
            for (int i = 0; i < cropVariety1.size(); i++) {
                System.out.println(cropVariety1.get(i));
                for (int b = 0; b < virusVariety.size(); b++) {
                    double varietyInt = 0.00;
                    double turr = 0.00;
                    if (virusVariety.get(b).indexOf(cropVariety1.get(i)) != -1) {
                        System.out.print("         " + virusVariety.get(b));

                        for (int c = 0; c < ccList1.size(); c++) {
                            // 判断是否是此病毒类别
                            if (ccList1.get(c).indexOf(virusVariety.get(b)) != -1) {
                                varietyInt++;
                                // 判断文件名是否与结果一致
                                if (fhList1.get(c).equals(ccList1.get(c))) {
                                    turr++;
                                }

                            }

                        }
                        double a = turr / varietyInt * 100;
                        System.out.println("此类别共有" + varietyInt + "个," + "正确" + turr + "个," + "正确率:"
                                + String.format("%.2f", a) + "%");

                        System.out.println();
                    }
                }

            }

            // System.out.println(fhList1.size());

            for (int i = 0; i < fhList1.size(); i++) {

                if (fhList1.get(i).equals(ccList1.get(i))) {
                    tur++;
                }

            }
            DecimalFormat df = new DecimalFormat("######0.00");
            double a = 969.00 / 1079.00 * 100;
            df.format(a);
            System.out.println("共有" + ccList1.size() + "个结果。正确的有:" + tur + "个,正确率有" + df.format(a) + "%。");
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

    }

}

 

效果图

 

 

 

apple
         apple_anthrax此类别共有19.0个,正确12.0个,正确率:63.16%

         apple_brown此类别共有19.0个,正确11.0个,正确率:57.89%

         apple_ring此类别共有20.0个,正确14.0个,正确率:70.00%

         apple_rot此类别共有20.0个,正确14.0个,正确率:70.00%

         apple_rust此类别共有20.0个,正确19.0个,正确率:95.00%

         apple_spotte此类别共有19.0个,正确19.0个,正确率:100.00%

corn
         corn_black此类别共有20.0个,正确20.0个,正确率:100.00%

         corn_health此类别共有20.0个,正确19.0个,正确率:95.00%

         corn_leaf此类别共有20.0个,正确20.0个,正确率:100.00%

         corn_rot此类别共有20.0个,正确19.0个,正确率:95.00%

         corn_rust此类别共有20.0个,正确18.0个,正确率:90.00%

         corn_sheath此类别共有20.0个,正确20.0个,正确率:100.00%

         corn_spot此类别共有20.0个,正确20.0个,正确率:100.00%

cucumber
         cucumber_anth此类别共有20.0个,正确18.0个,正确率:90.00%

         cucumber_downy此类别共有20.0个,正确15.0个,正确率:75.00%

         cucumber_health此类别共有20.0个,正确20.0个,正确率:100.00%

         cucumber_powdery此类别共有20.0个,正确20.0个,正确率:100.00%

         cucumber_target此类别共有20.0个,正确20.0个,正确率:100.00%

         cucumber_viral此类别共有20.0个,正确20.0个,正确率:100.00%

eggplant
         eggplant_gray此类别共有20.0个,正确17.0个,正确率:85.00%

         eggplant_health此类别共有20.0个,正确19.0个,正确率:95.00%

         eggplant_phy此类别共有20.0个,正确18.0个,正确率:90.00%

         eggplant_rphy此类别共有20.0个,正确18.0个,正确率:90.00%

         eggplant_spot此类别共有20.0个,正确19.0个,正确率:95.00%

         eggplant_viral此类别共有20.0个,正确20.0个,正确率:100.00%

         eggplant_wilt此类别共有20.0个,正确15.0个,正确率:75.00%

grape
         grape_anth此类别共有20.0个,正确17.0个,正确率:85.00%

         grape_black此类别共有20.0个,正确18.0个,正确率:90.00%

         grape_brown此类别共有20.0个,正确20.0个,正确率:100.00%

         grape_gray此类别共有20.0个,正确19.0个,正确率:95.00%

         grape_health此类别共有20.0个,正确20.0个,正确率:100.00%

         grape_powdery此类别共有20.0个,正确20.0个,正确率:100.00%

         grape_rot此类别共有20.0个,正确15.0个,正确率:75.00%

peanut
         peanut_blight此类别共有20.0个,正确20.0个,正确率:100.00%

         peanut_blotch此类别共有20.0个,正确16.0个,正确率:80.00%

         peanut_brown此类别共有20.0个,正确20.0个,正确率:100.00%

         peanut_health此类别共有20.0个,正确20.0个,正确率:100.00%

         peanut_rot此类别共有20.0个,正确20.0个,正确率:100.00%

         peanut_scab此类别共有20.0个,正确19.0个,正确率:95.00%

         peanut_viral此类别共有20.0个,正确20.0个,正确率:100.00%

potato
         potato_black此类别共有20.0个,正确20.0个,正确率:100.00%

         potato_early此类别共有20.0个,正确5.0个,正确率:25.00%

         potato_health此类别共有20.0个,正确20.0个,正确率:100.00%

         potato_late此类别共有20.0个,正确18.0个,正确率:90.00%

         potato_ring此类别共有20.0个,正确19.0个,正确率:95.00%

         potato_scab此类别共有20.0个,正确20.0个,正确率:100.00%

         potato_viral此类别共有20.0个,正确20.0个,正确率:100.00%

tomato
         tomato_blossom此类别共有20.0个,正确19.0个,正确率:95.00%

         tomato_early此类别共有20.0个,正确19.0个,正确率:95.00%

         tomato_gray此类别共有21.0个,正确21.0个,正确率:100.00%

         tomato_health此类别共有20.0个,正确20.0个,正确率:100.00%

         tomato_late此类别共有20.0个,正确8.0个,正确率:40.00%

         tomato_powdery此类别共有21.0个,正确19.0个,正确率:90.48%

         tomato_viral此类别共有20.0个,正确13.0个,正确率:65.00%

共有1079个结果。正确的有:969个,正确率有89.81%。
结果

 

日志文件下载

https://www.lanzous.com/i2b988d

 

 

 

 

 

posted on 2018-11-07 23:17  Zing_Z  阅读(1027)  评论(0)    收藏  举报

导航