title: Using FileWriter and BufferedWriter clearing file for some reason?
body: <p>For some reason, when I create a new BufferedWriter and FileWriter in my program (Even if I haven't used it to write anything yet), it clears the file I have selected of all it's text.</p>
  
  <p>selectedFile is determined by a JFileChooser.</p>
  
  <pre><code>public static File selectedFile;
  
      public static void Encrypt() throws Exception {
  
      try {
          //if I comment these two writers out the file is not cleared.
          FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile());
          BufferedWriter bw = new BufferedWriter(fw);
  
          List&lt;String&gt; lines = Files.readAllLines(Paths.get(selectedFile.toString()),
                  Charset.defaultCharset());
          for (String line : lines) {
              System.out.println(line);
              System.out.println(AESencrp.encrypt(line));
  
              /*file is cleared regardless of whether or not these are commented out or
               * not, as long as I create the new FileWriter and BufferedWriter the file
               * is cleared regardless.*/
  
              //bw.write(AESencrp.encrypt(line));
              //bw.close();
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>AESencrp.encrypt is just an encrypting class I have, it won't affect it. If I create a new FileWriter and BufferedWriter then this loop isn't even run (At least I do  not believe so, as I don't get the encryption of line or the original contents of the file printed, which prints if I haven't created the new FileWriter/BufferedWriter.)</p>
  
  <pre><code>        for (String line : lines) {
              System.out.println(line);
              System.out.println(AESencrp.encrypt(line));
  
              /*file is cleared regardless of whether or not these are commented out or
               * not, as long as I create the new FileWriter and BufferedWriter the file
               * is cleared regardless.*/
  
              //bw.write(AESencrp.encrypt(line));
              //bw.close();
          }
  </code></pre>