import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; /** * A small class to implement the beginning of a log file analyzer * This class mostly illustrates the use of hashtables for storing and * updating data * @author gtowell * Created: April 29, 2020 * */ public class LogAnalyzer { /** Holds lines from the log file. Lines are defined to be the * same if they have the same initial IP accress. * Uses the Java HashMap class as it has several function missing from * SepChainHT (and is probably a lot faster). */ private HashMap lineMap; /** * Initialization. */ public LogAnalyzer() { lineMap = new HashMap<>(); } /** * A basic reader of the log file. This is really only useful for * getting a count of the number of IPs of visitors * @param fileName the name of the web server log file */ public void readFile(String fileName) { try (BufferedReader br = new BufferedReader(new FileReader(fileName));) { String line; while (null != (line=br.readLine())) { LogLine ll = new LogLine(line); // Add a line into the hashmap, overwriting if anything // was there previously lineMap.put(ll.getIP(), ll); } } catch (FileNotFoundException fnf) { System.err.println(fnf.toString()); } catch (IOException ioe) { System.err.println(ioe.toString()); } catch (Exception eee) { System.err.println(eee.toString()); } } /** * A slightly more advanced reader. In this case it keeps count of the * number of visits from particular IP addresses. * @param fileName the name of the web log file */ public void readFileAndCount(String fileName) { try (BufferedReader br = new BufferedReader(new FileReader(fileName));) { String line; while ((null != (line=br.readLine()))) { LogLine ll = new LogLine(line); LogLine oll = lineMap.get(ll.getIP()); if (oll!=null) { oll.incCount(); } else { lineMap.put(ll.getIP(), ll); } } } catch (FileNotFoundException fnf) { System.err.println(fnf.toString()); } catch (IOException ioe) { System.err.println(ioe.toString()); } catch (Exception eee) { System.err.println(eee.toString()); } } /** * Print the IPs */ public void printIPs() { for (LogLine ll : lineMap.values()) { System.out.println(ll); } System.out.println("Number of IPS seen " + lineMap.size()); } /** * Print the IPs that visitems more than the given number of times * @param minCount the minimum number of times you must see the IP to print it. */ public void printIPCount(int minCount) { ArrayList vvv = new ArrayList(lineMap.values()); // if I wanted to sort, I now have the set in an array list, // from which sorting is fairly easy. int count=0; for (LogLine ll : vvv) { if (ll.getCount()>minCount) { System.out.println(ll.toStringLong()); count++; } } System.out.println("Number of IPS seen " + lineMap.size()); System.out.println("Number of IPS seen with count > " + minCount + ": " + count); } public static void main(String[] args) { LogAnalyzer la = new LogAnalyzer(); la.readFileAndCount("fields43.com-Apr-2020.txt"); la.printIPCount(30); } public static void main1(String[] args) { LogAnalyzer la = new LogAnalyzer(); la.readFile("fields43.com-Apr-2020.txt"); la.printIPs(); } }