title: BufferedReader/BufferedWriter Output line by line
body: <p>I have the following problem: I need to input a file with 12 lines. Each line consist of 8 characters. I have to output it in a file with 8 lines and 12 characters. I have to read the input line by line and output each line at the same time. So I'm not allowed to read my input first and after i read it just cut in in 8 lines with 12 characters. I'm using BufferedReader to read my file and BufferedWriter to write to my file. So by example:</p>
  
  <p>Input:<br>
  12345678<br>
  qwertyui<br>
  asdfghjk</p>
  
  <p>Output:<br>
  12345678qwer<br>
  tyuiasdfghjk</p>
  
  <p>Edit: It's an homework assignment indeed. </p>
  
  <pre><code>BufferedWriter bufferedWriter = null;
      FileReader fr;
  
      try {
          fr = new FileReader(new File(directory to file));
          bufferedWriter = new BufferedWriter(new FileWriter(directory to file);
  
          BufferedReader br = new BufferedReader(fr);
          String line = br.readLine();
  
          while (line != null) {
  
              bufferedWriter.write(output);
  
              bufferedWriter.newLine();
  
              line = br.readLine();
          }
  
          br.close();
  
      } catch (Exception e) {
          e.printStackTrace();
  
      } finally {
          //Close the BufferedWriter
          try {
              if (bufferedWriter != null) {
                  bufferedWriter.flush();
                  bufferedWriter.close();
              }
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  </code></pre>
  
  <p>This is how i read my inputfile and write to an outputfile, and it's the code I have at the moment.</p>