Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Files.write() : appending new lines in a text file

Tags:

java

java-8

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 ?

like image 565
FSm Avatar asked Jan 22 '17 09:01

FSm


People also ask

When a file is open for appending Where does write () put new text?

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.

How do you append a new line in a text file in Java?

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.

How do you add a new line to a text file in Python?

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.

How do I add a new line to text?

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.


1 Answers

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.

like image 64
davidxxx Avatar answered Oct 07 '22 19:10

davidxxx