淺談 System.getProperty("line.separator")
阿新 • • 發佈:2018-12-17
文章目錄
淺談 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”)代替 固定格式的換行符
- 具備平臺無關性
- 一次編寫,到處執行
- 更保險
3、注意點
並不是說具備了平臺無關性,保險這些好處就可以總是這麼用了。要根據具體情況來使用,比如說,在Windows 上開發的程式碼,執行在Linux/Unix 平臺下,但是使用者群體使用Windows 平臺。如果使用這個System.getProperty(“line.separator”),就有可能適得其反,導致使用者得到的結果不能換行,有利有弊,具體使用要看需求場景!