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());
    }
3
  • you could try to add \n for line breaks in files. e.g.: <books>\n <book publish..... Commented Feb 18, 2013 at 9:45
  • You should first make sure you have properly escaped the quotes in the string, such as those round "UTF-8". Commented Feb 18, 2013 at 9: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. Commented Feb 18, 2013 at 9:56

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
If you are unaware of the content, then the client MUST provide valid input since you are providing writing capabilities only.
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."));

Comments

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");

Comments

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.