I just made a very simple parser that gets input from a file and outputs to a file.
It does a line-by-line parsing of the file you give it and iterates the same function for all lines of the file and then it redirects the whole string to another file.
I give the parseLine
function a JsonObjectBuilder so that it can keep on adding json-like structures to the output string (it's a normal text to json parser btw).
parseFile function
private String parseFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader (file));
JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
String line;
try{
while((line = reader.readLine()) != null){
parseLine(line, jsonBuilder);
}
} finally {
reader.close();
}
return jsonBuilder.build().toString();
}
Parser class
public Parser(String file) throws IOException {
PrintWriter out = new PrintWriter(System.getProperty("user.dir") + "/parser/src/main/resources/logFormatted.log");
System.out.println("Initiated Parser class!");
out.println(parseFile(file));
}
parseLine function
private JsonObjectBuilder parseLine(String line, JsonObjectBuilder jsonBuilder){
String timestamp = null;
String eventCode = null;
String payload = null;
String finalString = null;
try {
timestamp = line.substring(0, 23);
eventCode = line.substring(24, 28);
payload = line.substring(29, line.length());
jsonBuilder.add(timestamp, Json.createObjectBuilder()
.add("eventCode", eventCode)
.add("payload", payload)
);
} catch (Exception ex) {
System.out.println("ERROR: " + ex);
}
return jsonBuilder;
}
Main
public static void main( String[] args ) throws IOException{
Parser parser = new Parser(System.getProperty("user.dir") + "/parser/src/main/resources/newFormatLog.log");
}
The problem with this code is that when it's executed it parses like it's supposed to but doesn't parse the whole file. Right now I'm giving it a ~4550 lines file but it does output a ~4400 line one.
I really don't know why and I'm starting to think the string is too big to handle or something. Maybe I need to output the lines immediatly to the file rather than doing a final big wite. All help is very appreciated!