简体   繁体   English

Java-将多行字符串写入文件

[英]Java - write multiline string to file

I have a string with multilines in it as shown below 我有一个包含多行的字符串,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<books>

    <book publishyear="1990">
        <name>Harry Potter</name>
    </book> 
</books>

how do I write this to a file? 如何将此写入文件? I have tried with buffered writer but, it doesn't take the string in multiline. 我已经尝试过使用缓冲编写器,但是它不采用多行字符串。

 try{
           FileWriter fstream = new FileWriter("D:/temp.txt");
           BufferedWriter out = new BufferedWriter(fstream);
           out.write(" <?xml version="1.0" encoding="UTF-8"?>
<books>

    <book publishyear="1990">
        <name>Harry Potter</name>
    </book> 
</books>");

           out.close();
  }catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }

Never ever write XML documents manually. 永远不要手动编写XML文档。 You will fail in file encoding, you will fail in syntax errors. 您将在文件编码中失败,在语法错误中将失败。 Always use DOM ore something similar. 始终使用类似的DOM矿石。 Your demo code already contains errors. 您的演示代码已包含错误。

This isn't related to writing a string or BufferedWriter. 这与编写字符串或BufferedWriter无关。

Java does not have multiline strings, so you'll have to concatenate strings if you want them on multiline in the source code. Java没有多行字符串,因此如果要在源代码中的多行字符串中将它们串联起来。 You also need to replace actual newlines with the escaped \\n character, and escape " with \\" 您还需要用转义的\\ n字符替换实际的换行符,并用“ \\”转义“

That is, you do: 也就是说,您可以:

String foo = "<?xml version="1.0" encoding=\"UTF-8\"?>\n"+
"<books>\n"+
"   <book publishyear=\"1990\">\n"+
"        <name>Harry Potter</name>\n"+
"    </book>\n"+
"</books>";

out.write(foo);

You can write all this on one line in your source code too if you want: 如果需要,您也可以在源代码的一行中编写所有这些代码:

out.write("<?xml version="1.0" encoding=\"UTF-8\"?>\n<books>\n ... etc.etc."));

You should escape the quotes and use a PrintWriter, which provides a println method. 您应该转引号,并使用提供Println方法的PrintWriter。

PrintWriter out = new PrintWriter(new BufferedWriter(fstream)); PrintWriter out =新的PrintWriter(new BufferedWriter(fstream));

out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<books>)";

Alternatively you can append line terminators to your string: 另外,您可以将行终止符附加到字符串中:

 out.write("<books>\r\n");

You can definitely write on a new line by using BufferedWriter 您绝对可以使用BufferedWriter在新行上写
See below code as an example, incorporate with your own logic (giving example of writing new line in a file write) 参见下面的代码作为示例,并结合您自己的逻辑(给出在文件写入中写入新行的示例)

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
writer.write("I am the first Line");
writer.newLine();
writer.write("I am in the second Line");

writer.close();  

You need to make the use of newLine() method present in BufferedWriter 您需要使用BufferedWriter存在的newLine()方法
Hope this helps and you don't have to change your code to new output stream :) 希望这会有所帮助,并且您不必将代码更改为新的输出流:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM