Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default Buffered Reader problem

    hi,

    I am trying to read a file twice. The sample code is as follows:

    Java Code:
    BufferredReader fileReader = new BufferedReader(new FileReader("abc.txt"));	
    	if(CiscoTraceRouteParser.canParse(fileReader))
    	{
                   // Do Something
    	}
    	else if(JuniperTraceRouteParser.canParse(fileReader))
    	{
    		// Do something else
    	}

    The canParse methods are of the type

    Java Code:
    public static boolean canParse(BufferredReader fileReader)
    {
      while((line == fileReader.readLine()) != null)
      {
          if(some condition satisfied for the line)
                  return true;
      }
    
      return false;
    }
    The problem is if the first 'if' check fails, then we have to do the second if check which when has to readLine from the first line of the same file. This is failing. I tried to mark and then do a reset , but not able to get the pointer reset to the begining of the file second time around.

    Please help me out with this. Thansk!

    Pradeep
    Last edited by Fubarable; 07-30-2009 at 06:26 PM. Reason: Code tags added for readability

  2. #2
    pushdown is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    i think making some changes as in the following solves the problem:
    Java Code:
    FileReader fileReader = new FileReader("abc.txt");
    	
    	if(CiscoTraceRouteParser.canParse(new BufferedReader(fileReader)))
    	{
                   // Do Something
    	}
    	else if(JuniperTraceRouteParser.canParse(new BufferedReader(fileReader)))
    	{
    		// Do something else
    	}

  3. #3
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks pushdown!
    It solves the problem. Looks like I cant use the same bufferredreader every time in the loop. Will use this for the time being.

    Regards,
    Pradeep

  4. #4
    pushdown is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by pradeep.theonlyone View Post
    Thanks pushdown!
    It solves the problem. Looks like I cant use the same bufferredreader every time in the loop. Will use this for the time being.

    Regards,
    Pradeep
    If BufferReader had a method which can be move the cursor to the beginning, then we could use same BufferReader object with moving cursor beginning, just before calling canParse func. but unfortunately it has not such method. Thus, we have to renew the reader object so that it can provide reading from beginning.

Similar Threads

  1. Please solve the problem related to PDf reader
    By kavithaprabhaker in forum New To Java
    Replies: 5
    Last Post: 11-23-2011, 10:08 AM
  2. Buffered Reader Exception
    By hitmen in forum New To Java
    Replies: 6
    Last Post: 01-07-2009, 11:14 AM
  3. Unable to draw buffered image
    By pedjasmek in forum Java 2D
    Replies: 7
    Last Post: 08-08-2008, 03:49 PM
  4. FileReader / Buffered Reader
    By sepaht in forum New To Java
    Replies: 9
    Last Post: 07-10-2008, 08:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •