2

I am creating one service without using database.

I am storing all my request data in file system.

But the issue is on every request it is creating the new file again and again but i want to put every request data in same file.

I am developing service using spring boot.

Allan Chua
  • 9,305
  • 9
  • 41
  • 61
Ravat Tailor
  • 1,193
  • 3
  • 20
  • 44

2 Answers2

1

You can have 2 files:

  1. In this file you append the content of the JSON string, one after another

  2. In this file you append the start position and end position of where in the file no.1 you have written the JSON, eachone in its own line. Here you can store other metadata also, like the request ID, the date and so on.

So, when you process the stored requests you read first the file no.2 to read the start and end positions and then you read from file no.1 from those positions.

Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54
  • can I just create one file and append into that and for every request I want to store data in next line of that file, tried to create file in @postconstruct method but that is not working – Ravat Tailor Sep 23 '18 at 07:33
  • you can do that but it's faster to use an "index" file like I've suggested; why it's not working? what error do you get? – Constantin Galbenu Sep 23 '18 at 07:41
1

You don't need spring boot for this one man, all you need to do is write to a buffered writer and add a line in it.

package com.pogs.utilities;

import org.joda.time.DateTime;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Class that implements request aggregation methods.
 *
 * @author Allan A. Chua
 * @version 1.0
 * @since August 31, 2018
 */
public class RequestAggregator {
    private final static String REQUEST_STORE_PATH = "D:\\logs.txt";

    //region Private Methods
    private void storeToTextFile(String input) {
        try {
            File file = new File(REQUEST_STORE_PATH);
            BufferedWriter fw = new BufferedWriter(new FileWriter(file.getAbsoluteFile(), true));

            fw.write(input);
            fw.newLine();

            fw.flush();
            fw.close();
        } catch (IOException ex) {
            System.out.println("[" + (new DateTime()).toString() + "][Logger is experiencing troubles]");
        }
    }
    //endregion

    //region Methods

    /**
     * Method used for appending request logs inside
     * the log aggregation method.
     *
     * @param input - Message to be logged.
     */
    public void appendInfo(String input) {
        String log = "[INFO][" + (new DateTime()).toString() + "]";
        log += input;

        storeToTextFile(log);
        System.out.println(log);
    }

    /**
     * Method used for appending error request logs inside
     * the log aggregation method.
     *
     * @param input Message to be logged.
     */
    public void appendError(String input) {
        String log = "[ERROR][" + (new DateTime()).toString() + "]";
        log += input;

        storeToTextFile(log);
        System.out.println(log);
    }
    //endregion
}
Allan Chua
  • 9,305
  • 9
  • 41
  • 61
  • 1
    yes got it, the problem was, I was not adding true in File-writer object for append thats why it was overriding the existing values – Ravat Tailor Sep 23 '18 at 08:49
  • Cool man! If you find the answer as helpful, you might want to mark it as your accepted answer to help other guys in the future ^_^ Glad you've fixed it – Allan Chua Sep 23 '18 at 08:50