\n 不起作用,不会换行

发布于 2024-11-06 02:18:56 字数 619 浏览 0 评论 0原文

我正在创建一个小程序,它将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盛夏尉蓝 2024-11-13 02:18:56

更具体地说,我建议使用:

x.format("%d%n%s%n", m.getPoints(), m.getStoreItem1Bought());

More specifically, I would recommend using:

x.format("%d%n%s%n", m.getPoints(), m.getStoreItem1Bought());
舂唻埖巳落 2024-11-13 02:18:56

改用这个:

public static String newline = System.getProperty("line.separator");

这两个选项都有效。您的问题在于如何格式化输出:

System.out.format("%s" + newline + "%s" + newline, "test1", "test2");
System.out.format("%s%n%s", "test1", "test2");

输出:

test1
test2
test1
test2

Use this instead:

public static String newline = System.getProperty("line.separator");

Both of this options work. Your problem is in how you format the output:

System.out.format("%s" + newline + "%s" + newline, "test1", "test2");
System.out.format("%s%n%s", "test1", "test2");

Output:

test1
test2
test1
test2
人间不值得 2024-11-13 02:18:56

使用格式时尝试使用 %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.

神魇的王 2024-11-13 02:18:56

这里没问题:

System.out.format ("first: %s%s", "" + x + "\n", "" + y + "\n");

虽然我更喜欢将 \n 集成到格式字符串中,而不是值:

System.out.format ("second: %s\n%s\n", x, y);

使用 Formatter.format 的工作原理是相同的。

No problem here:

System.out.format ("first: %s%s", "" + x + "\n", "" + y + "\n");

While I would prefere, to integrate the \n into the format String, not the values:

System.out.format ("second: %s\n%s\n", x, y);

Using Formatter.format works the same.

微暖i 2024-11-13 02:18:56

嗯,你的语法肯定很……有趣。如果您只是将字符串拼凑在一起,为什么还要使用格式化方法呢?另外,由于您无处透露您正在使用什么流,因此我们必须猜测一下,但无论如何。

无论如何,我打赌 1. 你使用的是 Windows,2. 你使用的编辑器(我打赌是记事本)只会对 \r\n 做出反应,因为这是 Windows 的正确换行符。要解决此问题,请不要在代码中对 \r\n 进行硬编码,而是使用 %n 并正确使用 printf 函数(即不要将字符串拼凑在一起!)。

否则,如果你真的必须将字符串拼凑在一起:

String newline = System.getProperty("line.separator");
x.format("%s", "" + m.getPoints() + newline);

会起作用。

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:

String newline = System.getProperty("line.separator");
x.format("%s", "" + m.getPoints() + newline);

will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文