title: Reading multiple records from a flat file in Java
body: <p>I have a text file dump that I need to convert to a delimited file. The file contains a series of "records" (for lack of a better word) formatted like this:</p>
  
  <pre><code>User: abc123 
  Date: 7/3/12
  Subject: the foo is bar
  Project: 123456
  Problem: foo bar in multiple lines of text
  Resolution: foo un-barred in multiple lines of text
  
  User: abc123 
  Date: 7/3/12
  Subject: the foo is bar
  Project: 234567
  Problem: foo bar in multiple lines of text
  Resolution: foo un-barred in multiple lines of text
  
  ...
  </code></pre>
  
  <p>My end result is to get a flat file of delimited values. Using the records above, we would see:</p>
  
  <pre><code>abc123;7/3/12;the foo is bar;123456;foo bar in multiple lines of text;foo un-barred in multiple lines of text
  abc123;7/3/12;the foo is bar;234567;foo bar in multiple lines of text;foo un-barred in multiple lines of text
  </code></pre>
  
  <p>Code appears below, and following that, the problem I'm experiencing.</p>
  
  <pre><code>    import java.util.*;
  import java.io.*;
  import java.nio.file.*;
  //
  public class ParseOutlookFolderForSE
  {
          public static void main(String args[])
          {
              String user = "";
              String PDLDate = "";
              String name = "";
              String PDLNum = "";
              String problemDesc = "test";
              String resolutionDesc = "test";
              String delim = ";";
              int recordCounter = 0;
              //
              try
              {
                  Path file = Paths.get("testfile2.txt");
                  FileInputStream fstream = new FileInputStream("testfile2.txt");
                 // Get the object of DataInputStream
                  /* DataInputStream in = new DataInputStream(fstream);  */
                  BufferedReader br = new BufferedReader(new InputStreamReader(fstream));  //Buffered Reader
                  String inputLine = null;     //String
                  StringBuffer theText = new StringBuffer();  //StringBuffer
  // problem: output contains last record ONLY. program is cycling through the entire file, overwriting records until the end.
  // add a for loop based on recordCounter
                  for(recordCounter=0;recordCounter&lt;10;recordCounter++)
                  {
                  while((inputLine=br.readLine())!=null)
                  {
                      if(inputLine.toLowerCase().startsWith("from:"))
                      {
  
                  /*      recordCounter = recordCounter++;    */  // commented out when I added recordCounter++ to the for loop
                          user = inputLine.trim().substring(5).trim();
                      }
                      else
                      if(inputLine.toLowerCase().startsWith("effective date"))
                      {
  
                          PDLDate = inputLine.trim().substring(15).trim();
                      }
                      else
                      if(inputLine.toLowerCase().startsWith("to:"))
                      {
  
                          name = inputLine.trim().substring(3).trim();
                      }
                      else
                      if(inputLine.toLowerCase().startsWith("sir number"))
                      {
  
                          PDLNum = inputLine.trim().substring(12).trim();
                      }
                  }      //close for loop
                  }   // close while
                  System.out.println(recordCounter + "\n" + user + "\n" + name + "\n" + PDLNum + "\n" + PDLDate + "\n" + problemDesc + "\n" + resolutionDesc);
                  System.out.println(recordCounter + ";" + user + ";" + name + ";" + PDLNum + ";" + PDLDate + ";" + problemDesc + ";" + resolutionDesc);
                  String lineForFile = (recordCounter + ";" + user + ";" + name + ";" + PDLNum + ";" + PDLDate + ";" + problemDesc + ";" + resolutionDesc + System.getProperty("line.separator"));
                  System.out.println(lineForFile);
                  try
                  {
                      BufferedWriter out = new BufferedWriter(new FileWriter("testfileoutput.txt"));
                      out.write(lineForFile);
                      out.close();
                  }
                  catch (IOException e)
                  {
                      System.out.println("Exception ");
                  }
              } //close try
              catch (Exception e)
              {
                  System.err.println("Error: " + e.getMessage());
              }
          }
  
      }
  </code></pre>
  
  <p>My final output is ONLY the last record. I believe that what's happening is that the program is reading every line, but only the LAST one doesn't get overwritten with the next record. Makes sense. So I added a <code>FOR</code> loop, incrementing by 1 <code>if(inputLine.toLowerCase().startsWith("user:"))</code> and outputting the counter variable with my data to validate what's happening.</p>
  
  <p>My <code>FOR</code> loop begins after step 3 in my pseudocode...after <code>BufferedReader</code> but before my <code>IF</code> statements. I terminate it after I write to the file in step 6. I'm using <code>for(recCounter=0;recCounter&lt;10;recCounter++)</code> and while I get ten records in my output file, they are all instances of the LAST record of the input file, numbered 0-9. </p>
  
  <p>Leaving the for loop in the same place, I modified it to read <code>for(recCounter=0;recCounter&lt;10;)</code> and placed <code>recCounter</code>'s increment WITHIN the <code>IF</code> statement, incrementing every time the line starts with <code>User:</code>. In this case, I also got ten records in my output file, they were ten instances of the last record in the input file, and all the counters are 0.</p>
  
  <p>EDIT: Given how the file is formatted, the ONLY way to determine w=one record from the next is a subsequent instance of the word "User:" at the start of the line. Each time that occurs, until the NEXT time it occurs is what constitutes a single record.</p>
  
  <p>It appears as though I'm not setting my "recCounter" appropriately, or I'm not interpreting the results of what IS being set as "start a new record".</p>
  
  <p>Anyone have any suggestions for how to read this file as multiple records?</p>