如何在 java 中写入每行末尾没有 cr 字符的文本文件

How to write a text file in java in which there is no cr character at the end of each line

我需要在 Java 中写一个 txt 文件,每行末尾没有回车 return 字符。 我已经在使用 PrintWriter。但输出如下: enter image description here

我需要像下图这样的。 enter image description here

我该怎么做?

实现此目的的一种方法是扩展 PrintWriter class 并覆盖 println() 方法。

public class UnixStylePrintWriter extends PrintWriter {
    @Override
    public void println() {
        write('\n');
    }
}

然后用这个代替正常的PrintWriter