title: Java Program to write text file, using separator, and in first line write record number
body: <p>I am trying to write in file, I need to write total number of the record in first line, and in while loop write all other line, everything working good but, in first line need to write total number of the record how can i do that, Please help me!! Thanks!!</p>
  
  <p>Here is my code: </p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileReader;
  import java.io.FileWriter;
  
  
  public class headerline {
  
      /**
       * @param args
       */
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
           File folderall = new File("FilesIn1");
           File[] BFFileall = folderall.listFiles();
  
           for (final File file : BFFileall) {
  
                  String str = file.getName();
  
                  BufferedReader br = null;
  
                   BufferedWriter lbwp = null; 
                   BufferedWriter lb = null;
  
                  try {
  
                      int lbwpcount = 1;
                      int lbcount = 1;
  
                       String reprintbwletterbwpca = (str);
                       lbwp = new BufferedWriter(new FileWriter(reprintbwletterbwpca));
                       lbwp.write("Total line number: " + lbwpcount);
  
                       String reprintbwletterbwpcalb = (str);
                       lb = new BufferedWriter(new FileWriter(reprintbwletterbwpcalb));
                       lb.write("Total line number: " + lbwpcount);
  
                       br = new BufferedReader(new FileReader(file));
  
                       String line;
                       line = br.readLine();
  
  
  
                       while ((line = br.readLine()) != null) {
  
                              String[] actionID = line.split("|");
  
                              String actionid  = actionID[2];
                              String custnumber = actionID[3];
  
                              lbwp.write("ActionID: " + actionid + ",CustomerNumber: " + custnumber + "\r\n");
                              lbwpcount++;
                              lb.write("ActionID: " + actionid + ",CustomerNumber: " + custnumber + "\r\n");
                              lbcount++;
                      }
                       lbwp.close();
                       lb.close();
  
                  } catch(Exception e1) {
                      e1.printStackTrace();
                  }   
  
           }
  
      }
  
  }
  </code></pre>
  
  <p>suppose file has 1201 lines, it should print 
  Total line number: 1200, in first line. 
  than
  "ActionID: " + actionid + ",CustomerNumber: " + custnumber
  ..........</p>
  
  <p>suppose other file has 1451 lines, it should print 
  Total line number: 1450, in first line. 
  than
  "ActionID: " + actionid + ",CustomerNumber: " + custnumber
  ..........</p>
  
  <p>I have no idea how can i do that, please help me!! can i write first line as last after finish while loop??
  Thanks in advanced!!</p>