Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.commons.csv.*;
- public class Parser {
- private static final Object [] FILE_HEADER = {"Packet Number", "Time", "Protocol", "Length"};
- public static void main(String[] args) {
- // Catch any errors that come from parsing the data
- try {
- // Call function with argument of Wireshark data location
- parseData("./wireshark_data.csv");
- } catch (Exception e) {System.out.println("Operation failed: " + e.toString()); }
- }
- // parseData:
- // arguments = url as String
- // exceptions = multiple possibilities
- // returns = nothing
- private static void parseData(String url) throws Exception {
- // Initialize file write with path for new csv file
- FileWriter fileWriter = new FileWriter("new_csv.csv");
- // Initialize Apache CSV file printer using our FileWriter
- CSVPrinter csvFilePrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withRecordSeparator("\n"));
- // Start by printing off the headers
- csvFilePrinter.printRecord(FILE_HEADER);
- // Initialize a Reader with our url
- Reader in = new FileReader(url);
- // Create an Iterable list of Apache CSVRecord's using the Default format with our Reader
- // Also specify that this csv file does have headers
- Iterable<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in);
- // Loop through each record
- for (CSVRecord record : records) {
- // Create a list of strings that will contains each element of packet data
- List<String> packetData = new ArrayList<String>();
- // Add each relevant component of packet data
- packetData.add(record.get("No."));
- packetData.add(record.get("Time"));
- packetData.add(record.get("Protocol"));
- packetData.add(record.get("Length"));
- // Add our list of packet data to the overall CSV file printer
- csvFilePrinter.printRecord(packetData);
- }
- // Flush and close the FileWriter and CSV FilePrinter
- fileWriter.flush();
- fileWriter.close();
- csvFilePrinter.close();
- }
- }
- // PacketData class with relevant fields
- class PacketData {
- private int packetNumber;
- private String protocol;
- private int length;
- private float time;
- // Constructor that sets initial values
- public PacketData(int packetNumber, String protocol, int length, float time) {
- this.packetNumber = packetNumber;
- this.protocol = protocol;
- this.length = length;
- this.time = time;
- }
- // Getters and Setters for each field
- public void setPacketNumber(int packetNumber) { this.packetNumber = packetNumber; }
- public int getPacketNumber() { return this.packetNumber; }
- public void setProtocol(String protocol) { this.protocol = protocol; }
- public String getProtocol() { return this.protocol; }
- public void setLength(int length) { this.length = length; }
- public int getLength() { return this.length; }
- public void setTime(float time) { this.time = time; }
- public float getTime() { return this.time; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement