title: Writing strings to a file in Java and reading them back
body: <p>I have a set of strings (<code>String</code> objects) in Java and would like to write them to a file so that I can later retrieve them.</p>
  
  <p>I understand that Java uses UTF-16 to store strings internally. I am worried that I might muck something up due to formatting issues unless I write and read the strings properly. I do not want to dump the String objects raw to the file as I would like to be able to open the file in a standard text editor to look at it, where each string is shown on its own line in a sensible way (assuming no string contains a line break).</p>
  
  <p>Can I simply use say the <code>PrintWriter</code> class with the <code>println(String x)</code> method (assuming there are no line breaks in the strings), combined with the Scanner class's <code>nextLine()</code> method when reading them back? Would this guarantee that I get the exact same strings back?</p>
  
  <p>Further, suppose the strings do contain line breaks, what is the appropriate way of writing them then? Should I filter out line breaks (replacing them with some ad-hoc escape code or similar) and use the <code>println</code> method with <code>PrintWriter</code> as above?</p>