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
Getting the new line character without System.getProperty("line.separator")?
提问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 czerny
Java 7 now has a System.lineSeparator()
method.
Java 7 现在有一个System.lineSeparator()
方法。
See Java: How do I get a platform independent new line character?
回答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”,您只需要将互联网上的字符串复制到您自己的常量中。