The screenshot of my intelliJ IDE as I could not post a picture directly (顺便说一下我使用的是jdk 10) 由于某种原因,我需要根据输入输出一个字符串,并且字符串中可能有新行。为了使程序在不同平台上运行,我使用一个变量来存储 System.lineSeparator() 并在需要时插入它。但后来我意识到 intelliJ 显示它已被弃用。所以我的问题是,为什么这个函数被弃用了?使用行分隔符是一种不好的做法吗?更好的方法是什么?
final String NEW_LINE = System.lineSeparator();
String output = "Line one" + NEW_LINE + "Line two";
WriteToFile(output);
更新:图为我的intelliJ IDE中的警告。该问题可能是由我的配置引起的。根据下面的回答(谢谢大家的回答),也许下次我应该先问“是不是”,然后再问“为什么”。
最佳答案
why is this function deprecated?
这是由 IntelliJ 中的插件、附加组件或配置设置实现的选择(正如一些评论指出的那样,他们的 IntelliJ 并没有将其视为已弃用)。据我所知,弃用这一点的唯一选择是它可能会带来兼容性错误。然而,从Java System class documentation:
lineSeparator
public static String lineSeparator()
Returns the system-dependent line separator string. It always returns the same value - the initial value of the system property line.separator.
On UNIX systems, it returns "\n"; on Microsoft Windows systems it returns "\r\n".
没有任何迹象表明 System.lineSeparator()
已被弃用,就其实现的版本 ( Java 7 ) 而言,直到最后一个可用版本 ( Java 10 )。如果它真的让你心烦意乱,你可能想要 ask the community directly .
Is it a bad practice using line separators?
完全不(据我所知,我可能是错的),特别是因为您使用的行分隔符与您运行代码的任何系统(理论上)兼容。
<小时/>What is the better way of doing this?
对于您对这段代码的设计选择,我没有更好的建议。看来Michal Lonski关于这一点,有一个。
关于java - 为什么 System.lineSeparator() 被弃用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49790574/