0

I have a GMF file. Its last 9 lines are to be considered as footer. The fast line of footer Starts with "Footer" and last line Ends with "End of file".

So far I have written this

while ((sCurrentLine = br.readLine()) != null ) {
                    if(headcount<11)
                    {
                        HeaderArray.add(sCurrentLine);
                        headcount++;
                        System.out.println("Header["+headcount+"]");
                        System.out.println("sCurrentLine"+sCurrentLine);
                    }
                    else if( headcount>10 &&(sCurrentLine.startsWith("Rerate"))) {


                                RecordsArray.add(sCurrentLine);
                                headcount++;
                                System.out.println("Records");
                                System.out.println("Record"+sCurrentLine);

                    }
                    else if(sCurrentLine.startsWith("Footer"))
                    {   
                            while(footerFlag!="End_of_file:")
                            {

                                FooterArray.add(sCurrentLine);
                                footerFlag=br.readLine();
                                sCurrentLine=footerFlag;
                                System.out.println("Footer");
                                System.out.println("Footer"+sCurrentLine);


                            }
                    }

            }

But this code only goes into an infinite loop. I need all 9 lines of footers in FooterArray. Please help.

2 Answers2

0

Can you please change while(footerFlag!="End_of_file:") to while(!footerFlag.equals("End_of_file:")) and see what will happen?

Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
  • Make this `while(!"End_of_file:".equals(footerFlag))` - footerFlag is probably unitialized or null at this point. And, by the way, footerFlag appears to be useless in this code - but I agree we do not have the full code here. – Ceredig Dec 12 '16 at 17:01
0

If third party libraries are an option for you the Apache Commons IO project has a class called ReversedLinesFileReader that will allow you to retrieve the footer of your file with minimal fuss.

I agree with @Anton that the .equals() method is the way to go to if you're trying to locate the EOF token. The way you've written your conditional statement is to see if the variable footerFlag has the exact memory location as the String literal "End_of_file:", which will never evaluate to true.

user6629913
  • 180
  • 1
  • 14