title: Avoid repetition when writing strings to text file line by line
body: <p>I use the following code to write strings to my simple text file:</p>
  
  <p><strong>EDITED:</strong></p>
  
  <pre><code>private String fileLocation="/mnt/sdcard/out.txt";
  
  public void saveHisToFile()
  {
      if (prefs.getBoolean("saveHis", true) &amp;&amp; mWordHis != null &amp;&amp; mWordHis.size() &gt;= 1)
      {
          StringBuilder sbHis = new StringBuilder();          
          Set&lt;String&gt; wordSet= new HashSet&lt;String&gt;(mWordHis);
          for (String item : wordSet)
          {
              sbHis.append(item);
              sbHis.append("\n");
          }
  
          String strHis = sbHis.substring(0, sbHis.length()-1);  
          try {
               BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
                       fileLocation), true));
               bw.write(strHis);
               bw.newLine();
               bw.close();
          } catch (IOException e) {
  
          }
      }
  }
  </code></pre>
  
  <p>The strings are successfully written to the text file, but weirdly, some strings are overwritten, such as:</p>
  
  <pre><code>apple
  orange
  grapes
  grapes
  grapes
  apple 
  kiwi
  </code></pre>
  
  <p>My question is: </p>
  
  <ol>
  <li>how can I stop a string being written more than once? </li>
  <li>how can I stop writing a string (a line) to the file if it has already existed in the file?</li>
  </ol>
  
  <p>I have consulted <a href="https://stackoverflow.com/a/13044285/998032">this post</a> but failed to apply it to my case. Can you please give a little help? Thanks a lot in advance. </p>