java 在没有 System.getProperty("line.separator") 的情况下获取换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3963582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:10:51  来源:igfitidea点击:

Getting the new line character without System.getProperty("line.separator")?

javafile-ionewline

提问by William

I want to create a text file, than load it up without any newlines or spaces (This is for a simple RPG). So I want to test for all 3 major OS line separators, and than the current OS'(s?) one. I know I can get the current one using System.getProperty("line.separator"), but how can I get Linux, Mac, and Windows line separators and turn them into a string for Java?

我想创建一个文本文件,而不是在没有任何换行符或空格的情况下加载它(这是一个简单的 RPG)。所以我想测试所有 3 个主要的操作系统行分隔符,而不是当前操作系统的(s?)一个。我知道我可以使用 System.getProperty("line.separator") 获取当前的行分隔符,但是如何获取 Linux、Mac 和 Windows 行分隔符并将它们转换为 Java 的字符串?

Edit: I need a single character representation of these, because for some reason "\n" doesn't work (Yes I'm on windows).

编辑:我需要这些的单个字符表示,因为出于某种原因“\n”不起作用(是的,我在 Windows 上)。

回答by RichieHindle

They are "\n"(Linux and MacOS X), "\r"(MacOS 9 and older) and "\r\n"(Windows).

它们是"\n"(Linux 和 MacOS X)、"\r"(MacOS 9 及更早版本)和"\r\n"(Windows)。

Just hard-code them - they're not going to change! :-)

只需对它们进行硬编码 - 它们不会改变!:-)

Edit:There's no "single character representation" of "\r\n"- it's two characters.

编辑:没有“单字符表示” "\r\n"- 它是两个字符。

回答by Rob Fonseca-Ensor

there's no "getter" for these, you just need to copy the strings off the internet into your own constants.

这些没有“getter”,您只需要将互联网上的字符串复制到您自己的常量中。