B. K. B. K. - 1 year ago 69
Java Question

How can I Write contents in the nextline within a File? and Finding a String inside a File and Replacing it at the same exact line

I am using a SpringBoot application and have introduced a "Like" button for Dog Pictures displayed and upon each Like it will hit the below

i am Concatenating the necessary information with a delimiter.

Method Inside the Controller shown below:

@RequestMapping(value = "likedDog")
public String likedDogList(@RequestParam("breedType") String breedType, @RequestParam("id") String id, Model model){

Long likes=0L;
String delimiter="_!_";

Dogs dogObject= dogsService.returnDogById(Long.valueOf(id));

dogObject.setLikes(likes);//set 1,2


String content=id+delimiter+breedType+delimiter+dogObject.getImgURL()+delimiter+dogObject.getLikes();
String contentSub=id+delimiter+breedType+delimiter+dogObject.getImgURL();

File file = new File("src/main/resources/data/DogLikes.txt");

if (!file.exists()) {

FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);

bw.flush(); // ORIGINAL
catch(Exception e){
System.out.println("Exception occured in likedDogList()");

return "liked";

I want to Write the Concatenated String "content" on a newline in the respective file if likes belong to different dogs, the code is shown below, even though I introduce
i cant write it in newline instead it simply replaces the old existing content in the first line of the file.

Contents in the File DogLikes.txt shown below:

17_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_1 //but on every click the contents are being replaced in this first line only

Expected Output, the contents to be written into DogLikes.txt on every Like is shown below:

18_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_1 //should be written like this in newline on like of dogId 18
19_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_1 //should be written like this in newline on like of dogId 19

Also if i click "Like" on DogId 18 it should go and replace the second line as follows,

18_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_2 //replacement of String in this exact location is expected

Your help on this is much appreciable, Thanks in advance.

hey @daniel the backup method worked for writing down the contents into the file line by line while i am using a different approach for restoring. However I came up with a new idea to incorporate both likes and dislikes into the same line as shown below:

Expected output on clicking "Like" and "Dislike"


I kinda need your help in backup() method as i tried different combinations to update likes and dislikes in the same line, as your approach dynamically updates only likes. Also I'm lookinf forward to sort the dogs images according to the highest likes to lowest.
Thank you for your help in advance.

Answer Source

As you described in your question your requirements were to use an in-memory storage and a backup mechanism to a particular file in the file system. I tried a short prototype that makes use of spring mvc and data rest. You can find here: https://github.com/dbubenheim/stackoverflow-40820684

The most interesting part is the backup and restore process of the data for which I have the following two service classes:

public class DogBackupServiceImpl implements DogBackupService {

    private String dogLikesFile;

    private static final String DELIMITER = "_!_";

    public void backup(final Dog dog) throws IOException {
        final Path dogLikesPath = Paths.get(this.dogLikesFile);
        if (!Files.exists(dogLikesPath)) {
        final File dogLikesFile = Paths.get(this.dogLikesFile).toFile();
        final StringBuilder stringBuilder = new StringBuilder();
        String currentLine;
        String oldLine = null;
        try (final Scanner scanner = new Scanner(dogLikesFile)) {
            while (scanner.hasNextLine()) {
                currentLine = scanner.nextLine().trim();
                if (currentLine != null && !currentLine.isEmpty()) {
                if (currentLine != null && currentLine.startsWith(Long.toString(dog.getId()))) {
                    oldLine = currentLine;
        String content = stringBuilder.toString();
        final boolean firstLike = (oldLine == null || oldLine.isEmpty());
        final String updateLine = dog.getId() + DELIMITER + dog.getBreedType() + DELIMITER + dog.getImgUrl() + DELIMITER + dog.getLikes();
        if (!firstLike) {
            content = content.replace(oldLine, updateLine);
        try (final BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(dogLikesFile))) {
            if (firstLike) {


public class DogRestoreServiceImpl extends ContextLoaderListener implements DogRestoreService {

    private String dogLikesFile;

    private static final int FIELD_ID = 0;
    private static final int FIELD_BREED_TYPE = 1;
    private static final int FIELD_IMG_URL = 2;
    private static final int FIELD_LIKES = 3;
    private static final String DELIMITER = "_!_";

    private final DogRepository dogRepository;

    public DogRestoreServiceImpl(final DogRepository dogRepository) {
        this.dogRepository = requireNonNull(dogRepository, "dogRepository must not be null!");

    public void restore() throws IOException {
        final Path path = Paths.get(this.dogLikesFile);
        if (!Files.exists(path)) {
            System.out.println("nothing to restore");
        final File dogLikesFile = path.toFile();
        final Set<Dog> dogs = new HashSet<>();
        String line;
        try (final BufferedReader reader = new BufferedReader(new FileReader(dogLikesFile))) {
            while ((line = reader.readLine()) != null) {
                final String[] fields = line.split(DELIMITER);
                final Dog dog = new Dog.DogBuilder(Long.parseLong(fields[FIELD_ID]))

    public void contextInitialized(final ServletContextEvent event) {
        try {
            System.out.println("context initialized. restoring data...");
            System.out.println("data restoring completed!");
        } catch (final IOException ioException) {

The idea is: The backup process saves every time the spring data repository saves an enitity to keep the backup file up to date. The restore process is basically triggered when the spring context is successfully initialized (when the application starts). So you should be able to have all the data both in memory and in the backup file.

You can interact with the service for example via cURL with the following basic actions:

# retrieve all dogs
curl -X GET "http://localhost:8080/dogs"

# post dog
curl -X POST -H "Content-Type: application/json" -d '{
    "breedType": "Husky",
    "imgUrl": "http://i.imgur.com/abc123.png",
    "likes": 0
}' "http://localhost:8080/dogs"

# like dog
curl -X POST -H "Content-Type: application/json" -d '' http://localhost:8080/dogs/likeDog/<dogs-id>"

Hope that helps! If there are any further questions let me know.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download