0

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());
    }
shockwave
  • 2,466
  • 7
  • 25
  • 49
  • you could try to add \n for line breaks in files. e.g.: \n – mooonli Feb 18 '13 at 09:45
  • You should first make sure you have properly escaped the quotes in the string, such as those round "UTF-8". – RoryB Feb 18 '13 at 09:47
  • put everything into a **StringBuffer/Builder** and then write the `toString()` version of it, in the file. String `concat` for so many lines, is troublesome. – RainMaker Feb 18 '13 at 09:56

4 Answers4

5

Never ever write XML documents manually. You will fail in file encoding, you will fail in syntax errors. Always use DOM ore something similar. Your demo code already contains errors.

Michael-O
  • 17,130
  • 6
  • 51
  • 108
  • Hi Michael, i have a string to be written into a file.the string can be of plaintext/xml/anything with multiline. So I don't know what type of String I get....So is it possible to write multiline string into a file? – shockwave Feb 18 '13 at 10:15
  • If you are unaware of the content, then the client MUST provide valid input since you are providing writing capabilities only. – Michael-O Feb 18 '13 at 10:21
3

This isn't related to writing a string or BufferedWriter.

Java does not have multiline strings, so you'll have to concatenate strings if you want them on multiline in the source code. You also need to replace actual newlines with the escaped \n character, and escape " with \"

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."));
nos
  • 207,058
  • 53
  • 381
  • 474
0

You should escape the quotes and use a PrintWriter, which provides a println method.

PrintWriter out = new 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");
Javier
  • 10,845
  • 4
  • 41
  • 52
0

You can definitely write on a new line by using 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
Hope this helps and you don't have to change your code to new output stream :)

asifsid88
  • 4,341
  • 18
  • 29