-4
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.

Nico S.
  • 77
  • 1
  • 1
  • 9

3 Answers3

5
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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
2

I had the same problem with that damn exception: Mark Invalid.

I came to this forum and I did not find something that would work so I had to figure out for myself what happens.

The BufferedReader creates a buffer (who would say it) from the moment I call the BufferedReader :: mark (int readAheadLimit) function, the size of the buffer IN CHARACTERS (NOT LINES) is given by readAheadLimit.

Once this limit is exceeded, it is not possible to go back with BufferedReader :: reset (), at the time of the mark because although the first data of the buffer could be recovered the later ones (those after the maximum of the buffer size and before the position current file) it would not be possible to retrieve them.

To avoid errors, the mark is invalidated by setting it to -2, and when the reset is called, the exception is produced: Mark Invalid. The issue is that it is NOT POSSIBLE with BufferedReader, to go back the pointer to read the file, it is simulated by storing the data in memory (the buffer).

So if you run into an Invalid Mark exception, you should make the buffer larger when calling the mark (int) method.

shinjw
  • 3,329
  • 3
  • 21
  • 42
2

For this, we need to understand how the BufferedReader works...

BufferReader will read 8192 characters at one shot and store it in a character buffer - cb (default value can be changed).

readLine() - will try to fetch the data from the character buffer, if it needs more characters than what is available, it will do a fetch again and fill the next 8192 characters...

mark() - is used to make the current line, so that using reset() method we can go back to the previously marked line.

mark() method takes a parameter - readAheadLimit, which should be the maximum number of characters in that line.

As given in the Java DOCS - having a limit less than the line size may fail. https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#mark(int)

If we try to fill the character array again after the character buffer array is exhausted and the number of pending characters is more than the marked limit (readAheadLimit). It will mark the markedChar as INVALIDATED. The next call to reset will lead to IOException - Mark invalid

CONCLUSION:

Make sure that the readAheadLimit is the maximum number of characters that can be present in a line.

If the line is already filled in the character buffer, you will not get any error since the check is done while trying to fill the character buffer again.

That is the significance of may fail in Java Docs.

Mathew Stephen
  • 309
  • 2
  • 7