在java中"\n"表示换行符。
但是不同系统中采用的换行符有所不同。
系统 | 换行符 |
---|---|
Windows | \n\r |
unix | \n |
mac | \r |
所以使用System.getProperty(“line.separator”);
可以换取当前系统的换行符,避免因为linux或Windows或Mac系统的不同导致运行报错。
System.getProperty(“line.separator”, “\n”);
表示如果根据key=line.separator获取不到值,则使用默认值"\n"。
java代码如下:
public static String getProperty(String key, String def) {
checkKey(key);
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPropertyAccess(key);
}
return props.getProperty(key, def);
}
public String getProperty(String key, String defaultValue) {
String val = getProperty(key);
return (val == null) ? defaultValue : val;
}