java.io.IOException:标记无效

Nic*_* S. -4 java markers reset ioexception

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();
        } ...
Run Code Online (Sandbox Code Playgroud)

为什么通过 if 语句后,markedChar 值会变为 -2?

谢谢尼科的每一个回答。

Jim*_*son 5

public void mark(int readAheadLimit)
      throws IOException
Run Code Online (Sandbox Code Playgroud)

标记流中的当前位置。对reset()的后续调用将尝试将流重新定位到这一点。

...

参数:

readAheadLimit- 限制在保留标记的同时可以读取的字符数。读取字符达到或超过此限制后尝试重置流可能会失败。大于输入缓冲区大小的限制值将导致分配一个大小不小于限制的新缓冲区。因此,应谨慎使用较大的值。

您将其设置readAheadLimit为 1 个字符,然后读取整行。这使得该标记无效。