Q:

Reading multiple lines from text file based on the starting prefix as record and then process it

I have text file as below 
6200MIGS Draft Capture              001527160719256                                                                                                                                                                                                             
622001004987654321098769    2030000000002385250000000000000000000000000719024031629498071912403119100719301201208620112629498      00QFCBA003353109297315583010   036000000190 100         000000    SHOEBRIDGE/DANIEL T MR       VI50 0818950138744  600110190 
6221QANTAS AIR                                                         AUSTRALIA       036             T                                                                                                                                                        
6223       0000000000000000000                                                                                                                                                                                                                                  
6229SHOEBRIDGE/DANIEL T MR                           0818950138744  QF  YNOGVJ                           15022602300045                                        -00000238525000000000000000000000000005                                                          
62301702281700BNE            MEL  QF  H XHLFEU           631                                000000000000000000000000000000000000                                                                                                                                
62311702282335MEL            DXB  QF  H XHLFEU             9                                000000000000000000000000000000000000                                                                                                                                
62321703011450DXB            LYS  QF  H OHLFEU          8081                                000000000000000000000000000000000000                                                                                                                                
62331703082115CDG            DXB  QF  O XOLREU1         8076 0818950138745                  000000000000000000000000000000000000                                                                                                                                
62341703091025DXB            BNE  QF  O  OLREU1         8434 0818950138745                  000000000000000000000000000000000000                                                                                                                                
622001005123456789012346    2030000000000575650000000000000000000000000719024031629499071912403117080719301201208620112629499      00QFCBA003353109297315583000   036000000190 100         000000    DETTMAN/GLEN MR              CA50 0818950138719  600110190 
6221QANTAS AIR                                                         AUSTRALIA       036             T                                                                                                                                                        
6222         0719           000                                                                                                                                                                                                                                 
6229DETTMAN/GLEN MR                                  0818950138719  QF  YYUGBM                           15022602300045                                        -00000057565000000000000000000000000002                                                          
62301703151800MEL            SYD  QF  L OLPTSCN01        458                                000000000000000000000000000000000000                                                                                                                                
62311703251830SYD            MEL  QF  M  MPTSCN01        463                                000000000000000000000000000000000000   

I want to read all lines starting with 6220 to the next 6220  in my case the lines are 2- 9 which i will call as a record. then i want to process each each line in the record based on the starting prefix for example if it starts with 6223 i will extract data what i need.
my question is how can i read the lines as record and then process it individually.
will mark and reset will be helpful .thanks in advance.The code which i have so far is 
    File infile = new File("C:\\Data\\Everyday work\\tmp\\CBRF.GEN4_FILE.20160719.txt");
    File outfile = new File("C:\\Data\\Everyday work\\tmp\\CBRF.GEN4_FILE.20160719.txt-DCFOUT.txt");
    FileReader reader;
    Scanner scanner;
    public void output() {

        try {

            FileWriter writer = new FileWriter(outfile);
            reader = new FileReader(infile);
            BufferedReader br = new BufferedReader(reader);
            String str;
            BufferedWriter bw = new BufferedWriter(writer);
            bw.flush();     
            while ((str = br.readLine() )!= null) {

                 scanner = new Scanner(str);

                String line = scanner.nextLine();
              if (line.startsWith("6220", 0)) {

                    recordtype = line.substring(0, 4);
                    msgrtypId = line.substring(4, 8);

                }     
          }   
   }   

here i can read line by line what i am trying to achieve is to read lines between line prefix 6220 and the next line prefix 6220 as a record.                                                                 

A:

Solving problems is typically about creating helpful abstractions. You are still at a point where you suppose you have to things "low level": your code reads a line from a file; and right there, you start processing it. 
So, lets step back and try to get on "higher ground":
First of all, do all "interesting" lines start with 6220? Or is the point that these lines have a specific format; that tells you that a new record begins? You see, that is very important; because if the later is true, you dont look for lines starting with 6220; but for lines that have the format of "record begins here" resp. "record ends here".
The next question is: what should happen to other lines?! Should they be ignored; or do you have to do other processing?
Then: is memory a constraint here? Meaning: would it be a good idea to simply read the complete file; and keep it in memory before doing any processing? Or is this a really large file; so large, that within your environment you should better not read the whole thing into memory?
You see, your question is lacking all this information; and seriously: things would get out of proportion if you tried to address all of that i a single question (or answer that is).
But let me give you some code to get started.
// before your loop starts
boolean currentlyParsingRecord = false;
boolean List<String> rawRecordLines = new ArrayList<>();

// within your loop
String line = scanner.nextLine();
if (currentlyParsingRecord) {
  if (line.startsWith("6220)) { 
    currentlyParsingRecord = false;
  }
  rawRecordLines.add(line);
} else {
  if (line.startsWith("6220)) { 
    currentlyParsingRecord = true;
    rawRecordLines.add(line);
    // and now, maybe you are calling a method like
    processRecordLines(rawRecordLines);
    // and then you clear your "buffer"
    rawRecordLines.clear();
  }
}

The point is: you have to somehow collect the lines that belong together. I am using a simple list here; and then you just pass that list around in order to process the data for that one record (and of course; you shouldn't forget about clearing that "buffer"; as you might start collecting the next one). Please note: of course this is not the final solution; it should only give you an idea how to go on from here. Such things can actually be pretty complicated to get right. And, you see; actually that is still a very "low-level" implementation; so there is a lot of room for improvement "OO design wise".
Hope that helps. But as said: before you further engage in programming; you better clarify your requirements. You should sit down; and talk to the people around you; to really understand how/what to process within that file. If you don't do that; chances are pretty high that whatever you will implement today won't be good enough any more tomorrow.

