如何在读取 Java 中的文件时摆脱无效标记错误?

How to get rid of Invalid Mark error while reading a file in Java?

我正在尝试读取一个文件,但我收到 IOException: Invalid mark error 当我 运行 代码时,即使它在例外。如果我增加标记的值(到 40 左右),它会生成完整且正确的输出,但会出现 NullPointerException。

这是我的代码:

     private static void readEventsFile2() throws FileNotFoundException, IOException {
        ArrayList<String> evtList = new ArrayList<>();
        FileReader fr=new FileReader("src/idse/Events.txt"); 
        BufferedReader br = new BufferedReader(fr);
        String a ="";

        try {
            
            while(!(a=br.readLine()).isEmpty()) {
                
                if (isNum(a)){ 
                    numEv = Integer.parseInt(a);
                    System.out.println(numEv);
                } else if(!a.isEmpty()){ 
                    String[] parts = a.split(":");
                    for (String part : parts) {
                        evtList.add(part);
                    }
                }
                br.mark(0);
                a = br.readLine();
                if(a == null || isNum(a)) { 
                    System.out.println(evtList);
                    evtList.clear();
                }
                br.reset();
            }
        } catch (NoSuchElementException | IllegalStateException | NullPointerException e) {
            System.out.println(e);
        }
        
    }   

上述代码的输出(第 149 行是 br.reset()):

5
[Logins, 2, Total time online, 1, Emails sent, 1, Orders processed, 1, Pizza’s ordered online, 0.5]
10
Exception in thread "main" java.io.IOException: Mark invalid
    at java.io.BufferedReader.reset(BufferedReader.java:512)
    at idse.IDSE.readEventsFile2(IDSE.java:149)
    at idse.IDSE.main(IDSE.java:188)

我正在阅读的文件格式:

5
Logins:2:Total time online:1:Emails sent:1:Orders processed:1:
Pizza’s ordered online:0.5:
10
Logins:7:Total time online:5:Emails sent:9:Orders processed:15:
Pizza’s ordered online:0.9:Logouts:6

mark()中的int参数表示要存入缓冲区的characters/bytes个数。如果读取的数据超过标记大小,那么 mark 将是 invalidated。在调用 reset() 时,它会给出该异常。

如果您阅读 reset() 的文档,它会抛出:

IOException - If the stream has never been marked,or if the mark has been invalidated

您可以通过增加标记参数的容量来修复您的代码。

br.mark(1000); // 1000 or depending on your buffer.

完整代码:

private static void readEventsFile2() throws FileNotFoundException, IOException {

    ArrayList<String> evtList = new ArrayList<>();
    FileReader fr = new FileReader("C:\Users\Abi.Shaquib\Desktop\overflow.txt");
    BufferedReader br = new BufferedReader(fr);
    String a = "";
    while ((a = br.readLine()) != null) {
        if (isNum(a)) {
            int numEv = Integer.parseInt(a);
            System.out.println(numEv);
        } else if (!a.isEmpty()) {
            String[] parts = a.split(":");
            for (String part : parts) {
                evtList.add(part);
            }
        }
        br.mark(1000);
        a = br.readLine();
        if (a == null || isNum(a)) {
            System.out.println(evtList);
            evtList.clear();
        }
        br.reset();
    }
}

输出:

5
[Logins, 2, Total time online, 1, Emails sent, 1, Orders processed, 1, Pizza\'s ordered online, 0.5]
10
[Logins, 7, Total time online, 5, Emails sent, 9, Orders processed, 15, Pizza\'s ordered online, 0.9, Logouts, 6]