简体   繁体   English

读取csv文件并比较每一行中数据的Java应用程序

[英]Java application that reads in a csv file and compares data in each row

I am fairly new to programming and have just been set the task of creating an application that will read in a .csv file name from a user input before then running a series of tests. 我对编程还不陌生,因此刚开始要创建一个应用程序,该应用程序将在执行一系列测试之前从用户输入中读取.csv文件名。 Ultimately the program needs to compare each row (each row in the csv file represents one router) and return which routers need a patch based on set criteria. 最终,程序需要比较每一行(csv文件中的每一行代表一个路由器),然后根据设置的标准返回需要修补的路由器。 The format of the CSV file will look like this with the headings 'Hostname', 'IP Address', 'Patched?', Os Version, and Notes which do not necessarily need to be included - CSV文件的格式看起来像这样,标题为“主机名”,“ IP地址”,“已打补丁?”,操作系统版本和注释,这些标题不一定要包括在内-

A.example.COM,  1.1.1.1, NO,    11,   Faulty fans
b.example.com,  1.1.1.2, no,    13,   Behind other routers
C.EXAMPLE.COM,  1.1.1.3, no,    12.1    
d.example.com,  1.1.1.4, yes,   14  
c.example.com,  1.1.1.5, no,    12,   Case a bit loose
e.example.com,  1.1.1.6, no,    12.3    
f.example.com,  1.1.1.7, No,    12.2    
g.example.com,  1.1.1.6, no,    15

So the program needs to return the name of any routers that do not share the same Hostname and IP Address as any other router, has a current OS version of 12 or higher, or has not already been patched. 因此,程序需要返回与其他路由器不共享相同主机名和IP地址,当前OS版本为12或更高版本或尚未打补丁的任何路由器的名称。

So far I have tried to read in each individual line as a Java object and compare from there however I have had no success. 到目前为止,我尝试将每一行作为Java对象读取并从那里进行比较,但是我没有成功。 Any suggestions on a good way to go about making this program work would be greatly appreciated, thanks. 非常感谢任何有关使该程序生效的好的建议。

First ask yourself what data structure would be best for your end result. 首先,问问自己哪种数据结构最适合最终结果。

  1. You need a structure that do not allow duplicates - id say a hashmap would be good for that. 您需要一个不允许重复的结构-id表示hashmap会很好。
  2. You have 3 criterias for enlisting the line in your output 您有3条条件将行加入到输出中
    • not patched - the easiest of them all criterias 未打补丁-其中最简单的所有条件
    • version over 12 still nothing hard about it 超过12的版本仍然没有什么困难
    • no duplicates 没有重复

Id advice getting clarification on what priorities for them - because a line can be "no" as patched, verstion over 9000, and have bazilion duplicates - what you do then ? Id建议会澄清它们的优先级-因为一条线打补丁可能会“不”,版本超过9000,并且有巴西文重复,那么您会怎么做? But lets say you have independant criterias - ie you check, you enlist two hashMaps union them. 但是,可以说您有独立的条件-即,您检查时,将两个hashMap结合起来。 One for the duplicates, one for the other two (more mechanical criterias). 一个用于重复,一个用于其他两个(更多的机械标准)。 The final result would be things that suit one of those criterias out there. 最终结果将是符合其中任何一项标准的事物。 You can build logic to check with the previous hashmaps - if it is already set in previous list - you can skip adding them. 您可以构建逻辑来检查以前的哈希图-如果已经在以前的列表中设置了哈希表,则可以跳过添加它们的步骤。 But this will be a tradeoff on using more CPU and less memory. 但这将是使用更多CPU和更少内存的折衷方案。 Depending of the size of your inputs you can decide what is better for you. 根据输入内容的大小,您可以决定什么对您更有利。

Now for checking for duplicates - you can check with a hashmap(very fast) for duplicate and add a counter to that record. 现在要检查重复项-您可以使用哈希图(非常快)检查重复项,并向该记录添加计数器。 When you finish with your input - those with counter == 1 would be the one you are looking for. 当您完成输入时-计数器== 1的输入将是您要查找的输入。

By the way - set up automatic loading,from file or directly from test code. 顺便说一句-从文件或直接从测试代码设置自动加载。 Typing those listings from a console is a tiresome and uneeded process while developing. 在开发过程中,从控制台键入这些列表是一个繁琐而无聊的过程。 Good luck. 祝好运。

Here are my suggestions: 这是我的建议:

  1. Create a class (let's call it "Router") that contains "hostName" - String, "ipAddresss" - String, "patched" - Boolean, "osVersion" - Double and "notes" - String. 创建一个包含“ hostName”-字符串,“ ipAddresss”-字符串,“ patched”-布尔值,“ osVersion”-双重和“ notes”-字符串的类(我们称其为“ Router”)。 Add related setters and getters. 添加相关的setter和getter。
  2. Create a list "routers" that contains a list of "Router" classes and initialize it to empty. 创建一个包含“路由器”类列表的“路由器”列表,并将其初始化为空。
  3. Create a method that takes a Router object as the parameter. 创建一个将Router对象作为参数的方法。 First compare the "osVersion". 首先比较“ osVersion”。 Then iterates through the "routers" list. 然后遍历“路由器”列表。 During the iteration compare the given Router object against each Router object you encounter. 在迭代期间,将给定的Router对象与您遇到的每个Router对象进行比较。 If there are duplicates don't do anything, otherwise add this object to the list after the iteration. 如果存在重复,则不执行任何操作,否则在迭代后将此对象添加到列表中。
  4. Iterate through each line of the CSV file and parse each row into "Router" object and invoke the method you created from #3. 遍历CSV文件的每一行,并将每一行解析为“ Router”对象,并调用您从#3创建的方法。
  5. The "routers" should contain the final result after the program finishes. 程序完成后,“路由器”应包含最终结果。

Working Solution for you csv format. 您的csv格式的有效解决方案 See how contains method works using equals and hashCode. 查看contains方法如何使用equals和hashCode进行工作。
You need to override equals and hashcode methods to compare and to use the contains() method 您需要重写equals和hashcode方法进行比较并使用contains()方法

Here is the solution for your csv that you have posted here. 这是您在此处发布的CSV解决方案。

package csv;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Shailesh Singh
 */
public class CSV {
    public static void main(String[] args) {
        List<Router> routers = new ArrayList<Router>();
        String fileToParse = "aaa.csv";
        BufferedReader fileReader = null;
        try
        {
            String line = "";
            fileReader = new BufferedReader(new FileReader(fileToParse));
            while ((line = fileReader.readLine()) != null) 
            { 
                String[] tokens = line.split("\\,", -1);
                //First level filter based on your requirement.
                if(Float.parseFloat(tokens[3])>=12 || tokens[2].toLowerCase().equals("no")){// this will check whether os version greater than 12 or pached no
                    routers.add(new Router(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4]));
                }
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        } 
        finally
        {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        List<Router> uniqueList = new ArrayList<Router>();
        System.out.println(routers);
        System.out.println("\n..........................................");
        for (Router router : routers) {
            //Loop through all the objects and store in a new unique array list
            //contains method works based on equals and hashcode
            if (!uniqueList.contains(router)) {
                uniqueList.add(router);
            }
        }
        System.out.println(uniqueList);
    }

}

Now Router.java 现在是Router.java

package csv;

/**
 *
 * @author Shailesh Singh
 */
public class Router {
    private String hostName;
    private String ip;
    private String patched;
    private String osVersion;
    private String note;

    public Router(String hostName, String ip, String patched, String osVersion, String note) {
        this.hostName = hostName;
        this.ip = ip;
        this.patched = patched;
        this.osVersion = osVersion;
        this.note = note;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    //Getters and setters.....................
    //Define all

    @Override
    public String toString() {
        return "Router{" + "hostName=" + hostName + ", ip=" + ip + ", patched=" + patched + ", osVersion=" + osVersion + ", note=" + note + '}'+"\n";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj == null || obj.getClass() != this.getClass()) {
            return false;
        }

        Router r = (Router) obj;
        if((hostName == r.hostName || (hostName != null && hostName.equals(r.getHostName()))) && (ip == r.ip || (ip != null && ip .equals(r.getIp())))){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((hostName == null) ? 0 : hostName.hashCode());
        result = prime * result
                + ((ip == null) ? 0 : ip.hashCode());
        return result;
    }


}

Notice that the equals() method and the hashCode() method overridden in Router Class. 请注意,在Router类中覆盖了equals()方法和hashCode()方法。

The important part of this Solution is 解决方案的重要部分是

............................
    List<Router> uniqueList = new ArrayList<Router>();
    System.out.println(routers);
    System.out.println("\n..........................................");
    for (Router router : routers) {
        //Loop through all the objects and store in a new unique array list
        //contains method works based on equals and hashcode
        if (!uniqueList.contains(router)) {//contains seeks equals and hashcode()
            uniqueList.add(router);
        }
    }
    System.out.println(uniqueList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM