\n 不起作用,不会换行
我正在创建一个小程序,它将 int 值保存到文本文件中,保存它,并在您再次启动程序时加载它。现在,我需要在文本文件中存储另外 3 个布尔值,我在文件中写入内容
public Formatter x;
x.format("%s", "" + m.getPoints() + "\n");
每当我想转到文本文件中的新行时,使用 \n,它不会转到新行,它直接将其写在 int 值后面。我尝试
x.format("%s", "" + m.getPoints() + "\n");
x.format("%s", "" + m.getStoreItem1Bought() + "\n");
同时
x.format("%s%s", "" + m.getPoints() + "\n", "" + m.getBought() + "\n");
执行这两个操作,但是,两者都只是将布尔值直接写在 int 值后面,而不开始新行。对此有什么帮助吗?
我使用的是 Windows 7 Ultimate 64 位,我的文本编辑器是 Eclipse,我也使用 Eclipse 运行所有代码。
I'm creating a small program that saves a int value into a text file, saves it, and loads it when you start the program again. Now, I need 3 more booleans to be stored in the text file, I am writing things in the file with
public Formatter x;
x.format("%s", "" + m.getPoints() + "\n");
Whenever I want to go to a new line in my text file, with \n, it wont go to a new line, it will just write it directly behind the int value. I tried doing both
x.format("%s", "" + m.getPoints() + "\n");
x.format("%s", "" + m.getStoreItem1Bought() + "\n");
and
x.format("%s%s", "" + m.getPoints() + "\n", "" + m.getBought() + "\n");
but, both will just write the boolean directly behind the int value, without starting a new line. Any help on this?
I am using Windows 7 Ultimate 64 bits, and my text editor is Eclipse, I am running all of the code with Eclipse too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更具体地说,我建议使用:
More specifically, I would recommend using:
改用这个:
这两个选项都有效。您的问题在于如何格式化输出:
输出:
Use this instead:
Both of this options work. Your problem is in how you format the output:
Output:
使用格式时尝试使用
%n
而不是\n
。有关详细信息,请参阅Formatter API 并在此页面搜索“行分隔符”,您就会看到。Try using
%n
instead of\n
when using format. For details on this, please see the Formatter API and search this page for "line separator" and you'll see.这里没问题:
虽然我更喜欢将 \n 集成到格式字符串中,而不是值:
使用 Formatter.format 的工作原理是相同的。
No problem here:
While I would prefere, to integrate the \n into the format String, not the values:
Using Formatter.format works the same.
嗯,你的语法肯定很……有趣。如果您只是将字符串拼凑在一起,为什么还要使用格式化方法呢?另外,由于您无处透露您正在使用什么流,因此我们必须猜测一下,但无论如何。
无论如何,我打赌 1. 你使用的是 Windows,2. 你使用的编辑器(我打赌是记事本)只会对 \r\n 做出反应,因为这是 Windows 的正确换行符。要解决此问题,请不要在代码中对 \r\n 进行硬编码,而是使用 %n 并正确使用 printf 函数(即不要将字符串拼凑在一起!)。
否则,如果你真的必须将字符串拼凑在一起:
会起作用。
Well your syntax is surely quite.. interesting. Why use the formatting method if you're just piece the string together anyways? Also since you nowhere say what stream you're using we have to guess a bit, but anyways.
Anyways I'm betting that 1. you're using windows and 2. that the editor (I bet on notepad) you're using only reacts to \r\n since that's the correct newline for Windows. To fix this DON'T hardcode \r\n in your code but instead use %n and use the printf function correctly (ie don't piece the string together!).
Otherwise if you really have to piece the string together:
will work.