浅谈 System.getProperty("line.separator")

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15071263/article/details/84652959

浅谈 System.getProperty(“line.separator”)


1、System.getProperty(“line.separator”) 是什么
// 获取操作系统对应的换行符
System.getProperty("line.separator")

java中的转义符"\r\n":
windows下的文本文件换行符:\r\n
linux/unix下的文本文件换行符:\r
Mac下的文本文件换行符:\n

2、为什么要用System.getProperty(“line.separator”)代替 固定格式的换行符
  1. 具备平台无关性
  2. 一次编写,到处运行
  3. 更保险
3、注意点

并不是说具备了平台无关性,保险这些好处就可以总是这么用了。要根据具体情况来使用,比如说,在Windows 上开发的代码,运行在Linux/Unix 平台下,但是用户群体使用Windows 平台。如果使用这个System.getProperty(“line.separator”),就有可能适得其反,导致用户得到的结果不能换行,有利有弊,具体使用要看需求场景!

猜你喜欢

转载自blog.csdn.net/qq_15071263/article/details/84652959