Files.write():在文本文件中追加新行 [英] Files.write() : appending new lines in a text file

查看:5635
本文介绍了Files.write():在文本文件中追加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码写入文本文件

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);

运行3次后,文本将保存到文本中:

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

请帮忙吗?

推荐答案

您应该避免使用特定的新行分隔符,例如\ n\\ n取决于操作系统,并支持使用在运行时应用的 System.lineSeparator()常量JVM运行的OS的分隔符。

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.

这篇关于Files.write():在文本文件中追加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆