title: Copying a file character by character in Java
body: <p>I'm working on an exercise where I must copy a file character by character in Java. I am working with the following file:</p>
  
  <pre><code>Hamlet.txt
  To be, or not to be: that is the question.
  Whether 'tis nobler in the mind to suffer
  The slings and arrows of outrageous fortune,
  Or to take arms against a sea of troubles,
  And by opposing end them ?
  </code></pre>
  
  <p>I create second file, called <code>copy.txt</code> which will contain a character by character copy of <code>Hamlet.txt</code> The problem is that after I run my code, <code>copy.txt</code> remains empty.</p>
  
  <pre><code>import java.io.FileWriter;
  import java.io.BufferedWriter;
  import java.io.PrintWriter;
  import java.io.FileReader;
  import java.io.BufferedReader;
  import java.io.IOException;
  
  public class Combinations {
      public void run() {
          try {
              BufferedReader rd = new BufferedReader(new FileReader("Hamlet.txt"));
          PrintWriter wr = new PrintWriter(new BufferedWriter(new FileWriter("copy.txt")));
          copyFileCharByChar(rd, wr);
      }catch(IOException ex) {
          throw new RuntimeException(ex.toString());
      }
  }
  
  private void copyFileCharByChar(BufferedReader rd, PrintWriter wr) {
      try {
          while(true) {
              int ch = rd.read();
              if(ch == - 1) break;
              wr.print(ch);
          }
        } catch(IOException ex) {
          throw new RuntimeException(ex.toString());
      }
      }
  
  public static void main(String[] args) {
      new Combinations().run();
  }
  }
  </code></pre>
  
  <p>So I write a method <code>copyFileCharByChar</code> that takes in a <code>BufferedReader</code> object <code>rd</code> and a <code>FileWriter</code> object <code>wr</code>. <code>rd</code> reads each individual character and the <code>wr</code> writes that corresponding character. What am I doing wrong here ?</p>