Search code examples
javaresetioexceptionmarkers

java.io.IOException: Mark invalid


public void createNewUser(String name, String passwort) {
        try {
            br = new BufferedReader(new FileReader("Data.txt"));
        } catch (FileNotFoundException brCreateError) {
            brCreateError.printStackTrace();
        }

        try {
            br.mark(1);
            System.out.println(br.readLine());
            try {
                if(br.readLine()==null) {
                    noUser=true;
                }else {
                    noUser=false;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            br.reset();

        } catch (IOException brMarkError) {
            brMarkError.printStackTrace();
        } ...

Why is the markedChar value changing to -2 after passing the if-statement?

Thx for every answer Nico.


Solution

  • public void mark(int readAheadLimit)
          throws IOException
    

    Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.

    ...

    Parameters:

    readAheadLimit- Limit on the number of characters that may be read while still preserving the mark. An attempt to reset the stream after reading characters up to this limit or beyond may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.

    You set the readAheadLimit to 1 character then read an entire line. This invalidated the mark.