title: Read one line at a time from a file and write to different files
body: <p>How to read one line at a time from a file and write to different files?</p>
  
  <p>Would like to know how to read one line at a time and write to different files. Can't find good example when googling:</p>
  
  <p>For instance, read a text file with following content:
      sent1
      sent2
      sent3
       ....</p>
  
  <p>write 
      sent1 to 1.txt,
      sent2 to 2.txt,
      sent3 to 3.txt</p>
  
  <p>It seems to be a simple operation, There is something wrong with my coding and not sure how to fix it.</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  public class MyFunction {
  public static void main(String args[])
  throws IOException, ClassNotFoundException
  {
  
  
     //variable initialization
  
    String strLine;
    int i = 0;
   //Read in a file and process one line at a time
  
     FileReader singleFile = new FileReader("C:\\data);
     BufferedReader reader = new BufferedReader(singleFile);
      for (int j=0; j &lt; 12; j++) {
     FileWriter outFile = new FileWriter("C:\\doc" + i + ".txt");
     BufferedWriter writer = new BufferedWriter(outFile);     
  
      //Read one line at a time from the input file
     while ((strLine = reader.readLine()) !=null) {
     //process a string which is not empty
  
     if (strLine.length() &gt; 0) {
  
     System.out.println("This is strLine: " + strLine);
     writer.write (strLine);
  
     i=i+1;
     System.out.println("This is i: " + i);
  
     } //end of if
     }
  
  
     singleFile.close();
     writer.close();
  
     } }}
  </code></pre>