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.