Java 句后加新行(为什么要加双新行来解决)?

Java add new line after a sentence ( why double new line to solve it)?

所以当我尝试做一些关于 IOFile 的练习时,我遇到了一个关于在 txt 文件上写字符串的问题,特别是在新文件中的每个句子之后写一个新行 (\n)。

这是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Main {
 public static void main(String[] args) {

    File Lyrics = new File("Lyrics.txt");
    File output = new File ("output.txt");

    try {
        Scanner myReader = new Scanner (Lyrics);
        try {
            FileWriter FBI = new FileWriter(output);
            while (myReader.hasNextLine()) {
                FBI.write(myReader.nextLine());
                FBI.write("\n");
            }
            FBI.close();
        }catch (IOException e) {}
        myReader.close();
    }catch (FileNotFoundException e) {}
  }
}

Lyrics.txt:

I could never find the right way to tell you
Have you noticed I've been gone
Cause I left behind the home that you made me
But I will carry it along

输出:

I could never find the right way to tell you
Have you noticed I've been gone
Cause I left behind the home that you made me
But I will carry it along
***invisible new line here

请求练习的输出:

I could never find the right way to tell you

Have you noticed I've been gone

Cause I left behind the home that you made me

But I will carry it along
***invisible new line here

在尝试添加新代码行并试图找出问题所在后,我简单地修改了有关在

中添加新行的代码行
FBI.write("\n\n");

但我仍然很困惑为什么我必须添加一个双换行 (\n\n) 来写句子后跟换行...

A \n 换行符在上一行的正下方开始一个新行,没有间隙。如果你想在句子之间有一个空行,你需要添加第二个 \n 来创建一个空行。

\n表示换行。

所以如果我的文字是

FooBar\nHello World

我会收到

FooBar
Hello World

自从 \n(新行)使我们的 HelloWorld 移动到新行以来,一切都是正确的。 但是你想要两个新行(当前一行+一个空白行)而不是一个,你必须使用 \n\n.

输入

FooBar\n\nHello World

输出

FooBar

Hello World