title: Java Files | Read a text file backwards and print result
body: <p>So here is the code:</p>
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Scanner;
  import java.nio.*;
  
  class FilePR1 {
  
      public static void main(String[] args) {
  
          Scanner in = new Scanner(System.in);
          System.out.println(&quot;Write input.txt &quot;);
          String inputFile = in.nextLine();
          System.out.println(&quot;Write output.txt &quot;);
          String outputFile= in.nextLine();
  
          if (inputFile .equals(&quot;input.txt&quot;) &amp;&amp; outputFile.equals(&quot;output.txt&quot;)) {    
              try {    
                  try {    
                      FileReader fr = new FileReader(&quot;input.txt&quot;);    
                      FileWriter fw = new FileWriter(&quot;output.txt&quot;);    
                      String str = &quot; &quot;;    
                      int i;    
                      System.out.println(&quot;Succes!  &quot;);
  
                      while ((i = fr.read()) != -1) { // Maybe I have to try a inverse reading of file. From the last line to first...
                                                      // ?????
                          str += (char) i;
                      }
  
                      fw.write(str); // OR maybe I can try an inverse printing. The program reads from the first line to the last , but i print the lines from the last to first...
  
                      fr.close();
                      fw.close();
  
                  } catch (FileNotFoundException e) {
                      System.out.println(&quot;I can't create the file! I cannot find the Input file!&quot;);
                      e.printStackTrace();
                  }
              } catch (IOException e) {    
                  System.out.println(&quot;Some errors... try again!&quot;);
              }
          } else {
              System.out.println(&quot;Files name are incorrect! &quot;);
          }
      }
  }
  </code></pre>
  <p><strong>In the input file we have for example</strong></p>
  <pre><code>Text1
  Text2
  Text3
  Text4
  Text5
  </code></pre>
  <p><strong>And at the output we have to obtain write:</strong></p>
  <pre><code>Text5
  Text4
  Text3
  Text2
  Text1
  </code></pre>
  <p><strong>P.S.</strong><br />
  I only managed to implement the part when we are reading a file and writing to another file. At the moment, I don't have any inspiration to continue ... to obtain the inverse version.</p>