java - 换行符不起作用

Pet*_*ter 0 java io

我正在编写一个将数据写入所选文件的简单程序.
除了换行符之外,一切都很顺利\n,字符串写在文件中但没有换行符

我试过了\n,\n\r但没有改变
程序:

    public void prepare(){
    String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n\r<data>\n\r<user><username>root</username><password>root</password></user>\n\r</data>";
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
    } catch (FileNotFoundException ex) {
        System.out.println("File Not Found .. prepare()");
    }
    byte b[] = content.getBytes();
    try {
        fos.write(b);
        fos.close();
    } catch (IOException ex) {
        System.out.println("IOException .. prepare()");
    }
}
public static void main(String args[]){
    File f = new File("D:\\test.xml");
    Database data = new Database(f);
    data.prepare();
}
Run Code Online (Sandbox Code Playgroud)

Mik*_*bel 5

Windows的行结尾遵循表单\r\n,而不是\n\r.但是,您可能希望使用与平台相关的行结尾.要确定当前平台的标准行结尾,您可以使用:

System.lineSeparator()
Run Code Online (Sandbox Code Playgroud)

...如果您运行的是Java 7或更高版本.在早期版本中,使用:

System.getProperty("line.separator")
Run Code Online (Sandbox Code Playgroud)