title: How can I read Swift message from file properly?
body: <p>Here I am using <a href="http://www.prowidesoftware.com/" rel="nofollow noreferrer">http://www.prowidesoftware.com/</a> liberary for parsing purpose.
  But I am not able to read proper file data means some block it left ,I don`t know why?</p>
  
  <p>Here I am unable to find out which type of meassage is there ,may be it mt940,mt950,mt300 like.</p>
  
  <p>Below I am pasting my code please review what is problem in my code.</p>
  
  <p>I am using these jar file </p>
  
  <p>pw-swift-core-SRU2016-7.8.5.jar
  commons-lang-2.6.jar</p>
  
  <pre><code>    import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.io.StringReader;
  
  
  import java.sql.SQLException;
  
  import java.text.ParseException;
  
  import java.util.ArrayList;
  
  
  import java.util.List;
  
  
  
  import com.prowidesoftware.swift.io.parser.SwiftParser;
  import com.prowidesoftware.swift.model.SwiftMessage;
  import com.prowidesoftware.swift.model.field.Field15A;
  import com.prowidesoftware.swift.model.field.Field15B;
  
  import com.prowidesoftware.swift.model.mt.mt3xx.MT300;
  
  
  public class SwiftMt300Parser {
      public static final String BLOCK_START_REG_EXP = "\\{1:";
      private static final String BLOCK_START = "{1:";
  
      public static void main(String[] args) throws Exception {
          SwiftMt300Parser Mt950Parser = new SwiftMt300Parser();
  
          String extPath = "D:\\FX-RECON\\";
  
          950Parser.parse(extPath);
  
      }
  
      public void parse(String folderLocation) {
  
          File mt950FolderLocation = new File(folderLocation+"\\SAMPLES261016NO2.txt");
          String mt950Files[] = mt950FolderLocation.list();
  
          SwiftParser swiftMsgParser = null;
  
          try {
  swiftMsgParser = new SwiftParser();
                  BufferedReader br = null;
              //if (mt950Files != null &amp;&amp; !folderLocation.isEmpty()) {
  
              //  for (String fileName : mt950Files) {
                      //System.out.println(fileName);
  
                      try {
                          String currentLine = "";
                          StringBuilder mtMessage = new StringBuilder();
                          List&lt;StringBuilder&gt; messageList = new ArrayList&lt;StringBuilder&gt;();
                          br = new BufferedReader(new FileReader(mt950FolderLocation));
  
                          while ((currentLine = br.readLine()) != null) {
  
                              currentLine = currentLine + "\n";
  
                              if (currentLine.contains(BLOCK_START)) {
  
                                  String[] str = currentLine.split(BLOCK_START_REG_EXP);
  
                                  for (int index = 1; index &lt; str.length; index++) {
                                      String token = str[index];
                                      if (index == 1) {
                                          mtMessage.append(token);
                                          messageList.add(mtMessage);
                                          mtMessage = new StringBuilder();
                                      } else if (index == (str.length - 1)) {
                                          mtMessage = new StringBuilder();
                                          mtMessage.append(BLOCK_START).append(token);
  
                                      } else {
  
                                          mtMessage = new StringBuilder();
                                          mtMessage.append(BLOCK_START).append(token);
                                          messageList.add(mtMessage);
  
                                      }
                                  }
                              } else {
  
                                  mtMessage = mtMessage.append(currentLine);
                              }
                          }
  
                          for (StringBuilder messageBuilder : messageList) {
                              if (messageBuilder != null) {
  
                                  swiftMsgParser.setReader(new StringReader(messageBuilder.toString()));
                                  SwiftMessage msg = swiftMsgParser.message();
  
                                  MT300 mt950 = new MT300(msg);
                                  persistMt950Dummy( mt950);
                              }
                          }
  
                      } catch (Exception e) {
                          e.printStackTrace();
                      } finally {
                          if (br != null) {
                              try {
                                  br.close();
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
                          }
                      }
                  //}
  
              //}
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
  
  
          }
      }
  long counter=1l;
      public int persistMt950Dummy( MT300 mt950) throws SQLException, ParseException {
          int insertCount = 0;
          try {
  
              System.out.println(mt950.getSender());
              System.out.println( mt950.getField30T());
              Field15A f=mt950.getField15A();
  
  
              Field15B f2=mt950.getField15B();
              if(mt950.getField20()!=null){
  
                  System.out.println( mt950.getField20().getValue());
  
              }
  
  
          } catch (Exception e) {
              //e.printStackTrace();
          }
          //System.out.println(counter1);
          return insertCount;
      }
  
  
  
  }
  </code></pre>
  
  <p>Below is my swift file Block on which first block is read but second block is not parse by the parser .</p>
  
  <pre><code>  {1:F01SWANBEB0AXXX0001000001}{2:I300SWANBEB0XXXXN}{3:{108:3000000244}}{4:
  :15A:
  :20:X
  :21:X
  :22A:AMND
  :94A:BROK
  :22C:SWANB00001SWANB0
  :17T:Y
  :17U:Y
  :82A:SWANBEB0
  :87A:SWANBEB0
  :83A:SWANBEB0
  :77D:X
  :15B:
  :30T:19970401
  :30V:19970401
  :36:1,
  :32B:USD15,
  :53A:SWANBEB0
  :56A:SWANBEB0
  :57A:SWANBEB0
  :33B:USD1,12
  :53A:SWANBEB0
  :56A:SWANBEB0
  :57A:SWANBEB0
  :58A:SWANBEB0
  :15C:
  :29A:/NAME/Ralph Whoit/DEPT/IRS
  /PHON/1-203-555-1212/TELX/555-1212
  /FAXT/1-203-555-1212
  :24D:ELEC
  :84A:CTFTUS33
  :85A:SWANBEB0
  :88A:SWANBEB0
  :71F:USD50,
  :26H:X
  :21G:X
  :72:/PHON/X
  /TELE/X
  /TELEBEN/X
  :15D:
  :17A:Y
  :32B:USD15,5
  :53A:SWANBEB0
  :56A:SWANBEB0
  :57A:SWANBEB0
  :58A:SWANBEB0
  :16A:1
  -} {1:F01SWANBEB0AXXX0001000001}{2:I300SWANBEB0XXXXN}{3:{108:3000000244}}{4:
  :15A:
  :20:X
  :21:X
  :22A:AMND
  :94A:BROK
  :22C:SWANB00001SWANB0
  :17T:Y
  :17U:Y
  :82A:SWANBEB0
  :87A:SWANBEB0
  :83A:SWANBEB0
  :77D:X
  :15B:
  :30T:19970401
  :30V:19970401
  :36:1,
  :32B:USD15,
  :53A:SWANBEB0
  :56A:SWANBEB0
  :57A:SWANBEB0
  :33B:USD1,12
  :53A:SWANBEB0
  :56A:SWANBEB0
  :57A:SWANBEB0
  :58A:SWANBEB0
  :15C:
  :29A:/NAME/Ralph Whoit/DEPT/IRS
  /PHON/1-203-555-1212/TELX/555-1212
  /FAXT/1-203-555-1212
  :24D:ELEC
  :84A:CTFTUS33
  :85A:SWANBEB0
  :88A:SWANBEB0
  :71F:USD50,
  :26H:X
  :21G:X
  :72:/PHON/X
  /TELE/X
  /TELEBEN/X
  :15D:
  :17A:Y
  :32B:USD15,5
  :53A:SWANBEB0
  :56A:SWANBEB0
  :57A:SWANBEB0
  :58A:SWANBEB0
  :16A:1
  -}
  </code></pre>
  
  <p>Thanks.</p>