How to append to the end of a file in Java?

...
...
Scanner scan = new Scanner(system.in);
System.out.println("Input : ");
String t = scan.next();

FileWriter kirjutamine = new FileWriter("...");
BufferedWriter out = new BufferedWriter(writing);
out.write(t)    
out.close();
...

If I write some strings in the file, it goes to the first line However, if you run the program again, it writes the new text to the previous text (enter the first line) I want to do this: if I insert something, it will go to the next line For example:

after 1 input) text1

after 2 input) text1

               text2

Wait

What should I change in the code? thank you!

Solution

java.io.PrintWriter pw = new PrintWriter(new FileWriter(fail,true));
java.io.PrintWriter pw = new PrintWriter(new FileWriter(fail,true));

This should be done Use it on an existing PW line

Editor: as explained in the comments, this leads to the following:

>Creating filewriter with optional 'append' flag set to true This causes the filewriter not to overwrite the file, but to open it to append and move the pointer to the end of the file. > Printwriter is using this filewriter (instead of creating its own file from the file you passed)

There are many editors here. I'm not sure about this problem several times

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>