0

I am trying to input a text into a notepad using the buffered writer and this is what codes I've come up with,

import java.util.Scanner;
import java.io.*;
public class FileSample {
    public static void main (String[] args) {
    Scanner sc = new Scanner(System.in);
    String yourtext = " ";
    String fn = "file.txt";
    String choice = " ";
    do{
    try{
        FileWriter fw = new FileWriter(fn);
        BufferedWriter bw = new BufferedWriter(fw);
        System.out.print("Enter text: ");
        yourtext = sc.nextLine(); 
        bw.write(yourtext);
        bw.newLine();

        bw.close();
        System.out.println("===================================");
        System.out.print("Do you still want to continue?:\n [Y]Yes \n [N]No 
      \n::");
        choice = sc.nextLine();
    }catch(IOException ex){
        System.out.println("Error writing to file '" + fn + "'");
    }
    }while(choice.equalsIgnoreCase("Y"));


}
}

so the problem is when the user wants to continue and enter a text again and completed the process the text that's supposed to be in the file.txt is replaced by the new entered text.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Pon
  • 41
  • 1
  • 5
  • Because you shouldn't create a new Writer that overwrites the file for every line the user inputs. You can configure the writer to append the file. But even so, you should probably keep the writer and stream opened until the user chose to not continue. – Jeremy Grand Aug 03 '17 at 08:44

2 Answers2

1

Your issue is just that you're opening the fileWriter in overwrite mode, to enable it to simply append new text to the existing file, just replace new FileWriter(fn) by FileWriter(fn,true) and that will fix it.

However, I've also noticed that you're sort of mishandling the resources (in my opinion), so I'd advise you to opened the Streams/Reader/Writer once, and close them at the end :

public static void main(String[] args) {
    String yourtext = " ";
    String fn = "file.txt";
    String choice = " ";
    try (Scanner sc = new Scanner(System.in);
            FileWriter fw = new FileWriter(fn);         // making sure to free resources after using them
            BufferedWriter bw = new BufferedWriter(fw);) {
        do {
            System.out.print("Enter text: ");
            yourtext = sc.nextLine();
            bw.write(yourtext);
            bw.newLine();
            System.out.println("===================================");
            System.out.print("Do you still want to continue?:\n [Y]Yes \n [N]No \n::");
            choice = sc.nextLine();
        } while (choice.equalsIgnoreCase("Y"));
    } catch (IOException ex) {
        System.out.println("Error writing to file '" + fn + "'");
    }
}
Jeremy Grand
  • 2,300
  • 12
  • 18
  • Might also be worth adding that having the scanner in try-with-resources means that System.in will also be closed at the end of the block. This is ok because the end of the block is at the end of main(). But if you want to do any further input after the block, you'll have to take the scanner out of the try-with-resources. – Klitos Kyriacou Aug 03 '17 at 09:00
0

Just add FileWriter(fn,true) this will keep the existing content and append the new content to the end of a file.

Laurynas Tamulevičius
  • 1,479
  • 1
  • 11
  • 25