I´m exporting data from a database table and writing it down in a file. Newline characters are correctly retrieved from the database and kept when writing the String (so I´m able to reimport the exported data).
My problem is that in the file the newline characters are implicit (i.e. they appear as line breaks). If the database value is the string “line1 \r\n line2”, the result of directly sending this String to my BufferedWriter + FileOutputStream is
“line1
line2”
Is there any way I can write to the file exactly the “line1 \r\n line2” sequence of characters? (I’ve already tried bw.write(value.toCharArray())
You can replace every escape sequence with a double-backslash:
Double- and single-apostrophe escaping can be added easily, but I’ve omitted it in this answer.