title: How to read or replace multiple lines of a file all at once?
body: <p>I am currently writing an encryption program that encrypts text documents with 64Bit encryption. The way it works is that it takes a string, and encrypts the string. I am currently searching for a way to have the program store all contents of a file in a string, encrypt the string, and then overwrite the file with the encrypted string. However, using</p>
  
  <pre><code>while((bufferedReader.readLine()) != null) {
  ...
  }
  </code></pre>
  
  <p>it only reads and encrypts the first line, and the rest of it remains untouched.</p>
  
  <p>however, using:</p>
  
  <pre><code>            List&lt;String&gt; lines = Files.readAllLines(Paths.get(selectedFile.toString()),
                  Charset.defaultCharset());
          for (String line : lines) {
          ...
          }
  </code></pre>
  
  <p>only the last line is encrypted. I honestly don't know what to do anymore, as I am kind of running out of ideas.</p>
  
  <p>Here is my current code (which also only appends to the file, as I was trying something new.) :</p>
  
  <pre><code>    public static void Encrypt() throws Exception {
  
      try {
  
          FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile(), true);
          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));
              bw.write(AESencrp.encrypt(line));
          }
  
          bw.close();
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>