Files.write (): add new lines to a text file

I use the following code to write to a text file

String content = "I Love Java";
Files.write(Paths.get(gg), (content + "\n").getBytes(UTF_8),StandardOpenOption.CREATE,StandardOpenOption.APPEND);

After 3 consecutive times, the text is saved in the text as follows:

I Love JavaI Love JavaI Love Java

But, I want the text in the text file to look like this:

I Love Java 
I Love Java
I Love Java

Any help please?

+4
source share
1 answer

You should avoid certain new line separators, such as "\n"or "\r\n", which are OS- specific and prefer to use the constant System.lineSeparator()that is used at run time for the OS separator that the JVM runs on.

+11
source

Source: https://habr.com/ru/post/1667470/


All Articles