【发布时间】:2014-05-27 19:19:53
【问题描述】:
我想将一些单词保存到 *.txt 文件中。但是有一个条件:每个单词都必须换行。所以我写了这个:
String content ="";
String nl = System.lineSeparator();
content += "my" + nl + "some" + nl + "random" + nl + "words";
但这段代码不起作用 - 所有单词都在同一行。
所以我尝试使用特殊字符 - \r\n:
String content ="";
content += "my" + "\r\n" + "some" + "\r\n" + "random" + "\r\n" + "words";
仍然没有用 - 在文件中所有单词都在同一行:
mysomerandomwords
除此之外:我的字符串content 被保存到文件中:
<a href="data:text/txt;charset=utf-8,<%=content%>" download="results.txt"><button class="button">Download</button></a>
我应该使用哪种分隔符将单词放在另一行中?
(我使用的是 Netbeans 8.0。文件在 Windows 记事本中打开)。
【问题讨论】:
-
尝试对
content进行URL编码。 -
避免使用windows-notepad。如果您不想使用 Notepad++ 之类的东西,请尝试使用 windows 写字板,至少它会显示行分隔符。