Search code examples
javajsonfilefilewriter

Write in Real Time and Remove last character in file JAVA


i'm converting a txt in json, and i'm using json-simple. I want the file to be written in real time, that is, each line, for this i chose to dont use JsonArray, because if I use the JSONArray, I have to wait for it to be complete first and then write it to the file. So I'm only using JsonObjects. The problem that I have to create a "hidden" JsonArray, for this at the beginning and end of file I add square brackets , and then to each JsonObject I add a comma. The problem that obviously the comma is printed even at the end of the file before "]", how do I remove the last comma?

    br = new BufferedReader(new FileReader(pathFile + ".txt"));
    JSONObject stringDetails = new JSONObject();
    JSONArray stringList = new JSONArray();
    try (FileWriter file = new FileWriter(pathfile+".json",true)) {
                    file.write("[");
                    while ((line = br.readLine()) != null) {
                       //Miss the code to convert from txt string to json string ...
                        stringDetails.put(stringJson.getKey(), stringJson.getMessage());
                        file.write(String.valueOf(stringDetails)+",");
                        stringDetails = new JSONObject();
                    }
                    file.write("]");
                }

A other question is, using the append (true), in case if the program stopped in anomaly way, all the strings before are saved?

Thanks so much.


Solution

  • I see two possible ways and both are essential the solution to not print it in the first place.

    First: use a boolean and an if-statement in the while loop to print the commas before the entries (except for the first one)

    boolean isFirst = true;
    
    file.write("[");
    while ((line = br.readLine()) != null) {
        //Miss the code to convert from txt string to json string ...
        stringDetails.put(stringJson.getKey(), stringJson.getMessage());
    
        if (isFirst) {
            isFirst = false;
        } else {
            // print the comma before the new entry
            file.write(",");
        }
    
        file.write(String.valueOf(stringDetails));
        stringDetails = new JSONObject();
    }
    file.write("]");
    

    Second: A second approch would be to have a private helper method to print the entry to the file, like:

    private static void printEntry(FileWriter file, String line, ... /* what ever else you need*/) {
        //Miss the code to convert from txt string to json string ...
        stringDetails.put(stringJson.getKey(), stringJson.getMessage());
        file.write(String.valueOf(stringDetails));
        stringDetails = new JSONObject();
    }
    

    and use this in to extract the writing for the first entry from the while loop, like:

    file.write("[");
    if ((line = br.readLine()) != null) {
        // print first entry
        printEntry(file, line, ...);
    
        // print the rest
        while ((line = br.readLine()) != null) {
            file.write(",");
    
            printEntry(file, line, ...);
        }
    }
    file.write("]");