从二进制文件读取后,我的程序不断抛出IO异常(My program keeps throwing IO exception after reading from binary file)

编程入门 行业动态 更新时间:2024-10-08 05:33:45
从二进制文件读取后,我的程序不断抛出IO异常(My program keeps throwing IO exception after reading from binary file)

我已经接收了一个文本文件并通过将对象写入二进制文件将其转换为二进制文件,然后我尝试从二进制文件读取,该文件一直工作直到最后一条记录然后它抛出IO异常从来没有得到我最后的印刷声明。

下面是我触发错误的代码

try { ObjectInputStream objinputStream2 = new ObjectInputStream(new FileInputStream("binaryFile")); for (int run = 0; run<count;count++) { Record readone = (Record) objinputStream2.readObject(); System.out.print(readone); System.out.println(""); } System.out.println("Reading completed for all" + count + " records. "); // objinputStream2.close(); } catch(FileNotFoundException e){ // Catches object System.out.println("Sorry File not found"); // Error Message } catch(ClassNotFoundException e){ // Catches object System.out.println("Sorry class not found"); // Error Message } catch(IOException e){ // Catches object System.out.println("Problem with file output."); // Error Message e.printStackTrace(); }

任何帮助表示赞赏

堆栈跟踪:

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at bluej.runtime.ExecServer$3.run(ExecServer.java:730)

I have taken in a text file and converted it into a binary by writing the objects to a binary file, then i am attempting to read from the binary file, which works all the way through till the last record and then it throws the IO exception and never gets to my last print statement.

Below is my code that is triggering the error

try { ObjectInputStream objinputStream2 = new ObjectInputStream(new FileInputStream("binaryFile")); for (int run = 0; run<count;count++) { Record readone = (Record) objinputStream2.readObject(); System.out.print(readone); System.out.println(""); } System.out.println("Reading completed for all" + count + " records. "); // objinputStream2.close(); } catch(FileNotFoundException e){ // Catches object System.out.println("Sorry File not found"); // Error Message } catch(ClassNotFoundException e){ // Catches object System.out.println("Sorry class not found"); // Error Message } catch(IOException e){ // Catches object System.out.println("Problem with file output."); // Error Message e.printStackTrace(); }

any help is appreciated

Stack trace:

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at bluej.runtime.ExecServer$3.run(ExecServer.java:730)

最满意答案

您必须重新考虑循环条件: for (int run = 0; run<count;count++) 。

在循环内部都没有修改run或count ,因此它将永远运行(直到你尝试从文件中读取记录并发生Exception )。

我认为应该是这样的:

int count = 0; for (int run = 0; run<numberOfRecordsInFile; run++) { Record readone = (Record) objinputStream2.readObject(); System.out.print(readone); System.out.println(""); count++; } System.out.println("Reading completed for all" + count + " records. ");

You have to rethink the loop condition: for (int run = 0; run<count;count++).

Neither run nor count are modified inside the loop, so it will run forever (until you try to read a record from your file and an Exception occurs).

I think it should be something like:

int count = 0; for (int run = 0; run<numberOfRecordsInFile; run++) { Record readone = (Record) objinputStream2.readObject(); System.out.print(readone); System.out.println(""); count++; } System.out.println("Reading completed for all" + count + " records. ");

更多推荐

本文发布于:2023-08-03 22:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1400890.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   异常   二进制文件   程序   IO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!