1. 程式人生 > >淺談 System.getProperty("line.separator")

淺談 System.getProperty("line.separator")

文章目錄

淺談 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”),就有可能適得其反,導致使用者得到的結果不能換行,有利有弊,具體使用要看需求場景!