Scott Kocos
Scott Kocos

Reputation: 28

Reset Java BufferedReader after parsing

I know this is done with mark() and reset(). However I'm very new to reading in files and my code doesn't work and I know my mistake is probably pretty obvious and stupid I just can't figure it out. I've tried moving things around it just hasn't worked out. The goal of my code is to pop up a joption pane the first time a null line is found(which i haven't gotten to yet), and then return to the top of the file. This is probably a dumb question but if someone can help me out I'd really appreciate it. By the way the issue is getting the IOException thrown after the last line is read( It executes this method each time a jbutton is clicked ).

public static void fileReader( String inputFile , JTextField array[] )
{
    /** Local Constants **/

    final String NOT_READABLE = "File not readable!!";
    final String IO_ERROR     = "Input/Output Error!!";

    /** Local Variables **/

    String line;

    if ( inStream == null )
    {
        fileOpen( inputFile );
    }

    if ( isReadableFile( inputFile ) )
    {
       try
       { 

         line = inStream.readLine();
         inStream.mark( 64 );

         if ( line != null )
         {
            int j = 0;

            for ( int i = 0; i < line.length(); i++ )
            {
                if ( Character.isDigit( line.charAt( i ) ) )
                {
                    if ( j < array.length )
                       array[ j ].setText( line.charAt( i ) +
                                           MagicSquareGUI.BLANK );

                    j++;
                }
            }
         }

         else
         {
            inStream.close();
            inStream.reset();
         }
     }

     catch ( IOException e )
     {
        System.out.println( IO_ERROR );
     }

     catch ( Exception e )
     {
        System.out.println( ERROR );
     }
  }

  else
     System.out.println( NOT_READABLE );

}

Upvotes: 0

Views: 479

Answers (3)

Scott Kocos
Scott Kocos

Reputation: 28

Problem solved. I moved the mark() into the fileOpen() method called within the method above. It now marks properly and resets the way it's supposed to.

Upvotes: 0

Ravi Trivedi
Ravi Trivedi

Reputation: 2360

You are getting this error IOException after you try resetting the stream.

Possibilities:

1) The reset you are calling on the type of stream class may not be supported. Reset is not supported by all types of streams.

2) You have already read the bytes. In this case, Reset will fail. Reset is supported only if the bytes are not already read more than the mark limit. Check your line variable and its length if you are already reading the bytes.

Answer Updated:

1) You need to mark before you start reading. It should be like this:

     inStream.mark( 64 );
     line = inStream.readLine();

2) Don't close the stream unless you are finished with it. Closing the stream and then use Reset is wrong.

This line inStream.close() needs to be removed from current else and put it in the end of the function, probably after last else.

3) One more thing I want to point out to you is that you are only reading 1st line in the text file. Your stream reader is not in the loop.

I tested your function successfully with the mentioned changes.

Upvotes: 1

user207421
user207421

Reputation: 310980

You can't reset a stream or Reader after you close it. Leave it open.

Upvotes: 0

Related Questions