【问题标题】:How to get rid of Invalid Mark error while reading a file in Java?在 Java 中读取文件时如何摆脱 Invalid Mark 错误?
【发布时间】:2020-11-10 10:27:31
【问题描述】:

我正在尝试读取文件,但运行代码时却收到 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

【问题讨论】:

    标签: java bufferedreader


    【解决方案1】:

    mark() 中的 int 参数表示要存储在缓冲区中的字符/字节数。如果您读取的数据过多超出了标记的大小,则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]
    

    【讨论】:

    • 我增加了标记参数的容量,但我开始得到 NullPointerException
    • while ((a = br.readLine()) != null) { 在你的 while 循环中使用它。 isEmpty 不检查 null
    • 你给的 int 值是多少?你的输入是多久??
    • 我在底部粘贴的格式是文件中的内容。我尝试了许多从 0 到数千的标记值。它们都不起作用我要么得到 Invalid mark 要么 NullPointerException。
    • 谢谢,我尝试了您发布的代码,现在可以使用了!我之前什至尝试过大于 1000 的值,但那次没有用。
    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    相关资源
    最近更新 更多