Advertisement
Guest User

407

a guest
Apr 28th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. import org.apache.commons.csv.*;
  6.  
  7. public class Parser {
  8.     private static final Object [] FILE_HEADER = {"Packet Number", "Time", "Protocol", "Length"};
  9.  
  10.     public static void main(String[] args) {
  11.         // Catch any errors that come from parsing the data
  12.         try {
  13.             // Call function with argument of Wireshark data location
  14.             parseData("./wireshark_data.csv");
  15.         } catch (Exception e) {System.out.println("Operation failed: " + e.toString()); }
  16.     }
  17.    
  18.     // parseData:
  19.     //          arguments = url as String
  20.     //          exceptions = multiple possibilities
  21.     //          returns = nothing
  22.     private static void parseData(String url) throws Exception {
  23.         // Initialize file write with path for new csv file
  24.         FileWriter fileWriter = new FileWriter("new_csv.csv");
  25.        
  26.         // Initialize Apache CSV file printer using our FileWriter
  27.         CSVPrinter csvFilePrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withRecordSeparator("\n"));
  28.        
  29.         // Start by printing off the headers
  30.         csvFilePrinter.printRecord(FILE_HEADER);
  31.  
  32.         // Initialize a Reader with our url
  33.         Reader in = new FileReader(url);
  34.        
  35.         // Create an Iterable list of Apache CSVRecord's using the Default format with our Reader
  36.         // Also specify that this csv file does have headers
  37.         Iterable<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in);
  38.        
  39.         // Loop through each record
  40.         for (CSVRecord record : records) {
  41.            
  42.             // Create a list of strings that will contains each element of packet data
  43.             List<String> packetData = new ArrayList<String>();
  44.            
  45.             // Add each relevant component of packet data
  46.             packetData.add(record.get("No."));
  47.             packetData.add(record.get("Time"));
  48.             packetData.add(record.get("Protocol"));
  49.             packetData.add(record.get("Length"));
  50.            
  51.             // Add our list of packet data to the overall CSV file printer
  52.             csvFilePrinter.printRecord(packetData);
  53.         }
  54.        
  55.         // Flush and close the FileWriter and CSV FilePrinter
  56.         fileWriter.flush();
  57.         fileWriter.close();
  58.         csvFilePrinter.close();
  59.  
  60.     }
  61.  
  62. }
  63.  
  64. // PacketData class with relevant fields
  65. class PacketData {
  66.     private int packetNumber;
  67.     private String protocol;
  68.     private int length;
  69.     private float time;
  70.    
  71.     // Constructor that sets initial values
  72.     public PacketData(int packetNumber, String protocol, int length, float time) {
  73.         this.packetNumber = packetNumber;
  74.         this.protocol = protocol;
  75.         this.length = length;
  76.         this.time = time;
  77.     }
  78.    
  79.     // Getters and Setters for each field
  80.     public void setPacketNumber(int packetNumber) { this.packetNumber = packetNumber; }
  81.     public int getPacketNumber() { return this.packetNumber; }
  82.    
  83.     public void setProtocol(String protocol) { this.protocol = protocol; }
  84.     public String getProtocol() { return this.protocol; }
  85.    
  86.     public void setLength(int length) { this.length = length; }
  87.     public int getLength() { return this.length; }
  88.    
  89.     public void setTime(float time) { this.time = time; }
  90.     public float getTime() { return this.time; }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement