我已经接收了一个文本文件并通过将对象写入二进制文件将其转换为二进制文件,然后我尝试从二进制文件读取,该文件一直工作直到最后一条记录然后它抛出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. ");更多推荐
发布评论