java - 为什么 System.lineSeparator() 被弃用?

标签 java intellij-idea

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/

相关文章:

java - 构造函数 BitmapDrawable() 已弃用修复

java - 使用来自 angular4 的 @RequestPart 在一个请求中发布多部分文件和 json

java - 使用 IntelliJ 调试 GWT

intellij-idea - 使用IntelliJ编译Java 7,配置麻烦

user-interface - 如何将 NetBeans 表单及其类转换为可在 IntelliJ IDEA 表单编辑器中使用的内容?

java - 如何为逐项叠加项创建自定义对话框?

java - Swing 容器和组件之间的父子关系

version-control - 合并 IntelliJ IDEA .IPR 和 .IWS 文件

java - 如何限制对 Arraylist 的访问并禁止更改?

intellij-idea - PhpStorm 检查器无法识别 XML 属性中的冒号