I'm using the code below to write into a text file
String content = "I Love Java";
Files.write(Paths.get(gg), (content + "\n").getBytes(UTF_8),StandardOpenOption.CREATE,StandardOpenOption.APPEND);
After 3 times of run, the text is saved into the the text as :
I Love JavaI Love JavaI Love Java
But, I want the text in the text file looks like:
I Love Java
I Love Java
I Love Java
Any help please ?
You can use FileWriter to open the file for appending text as opposed to writing text. The difference between them is that when you append data, it will be added at the end of the file. Since FileWriter writes one character at a time, it's better to use BufferedWriter class for efficient writing.
In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.
To add spacing between lines or paragraphs of text in a cell, use a keyboard shortcut to add a new line. Click the location where you want to break the line. Press ALT+ENTER to insert the line break.
You should avoid specific new line separators such as "\n"
or "\r\n"
that depends on the OS and favor the use of the System.lineSeparator()
constant that applies at runtime the separator of the OS on which the JVM runs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With