It looks like you're using an Ad Blocker.

Please white-list or disable AboveTopSecret.com in your ad-blocking tool.

Thank you.

 

Some features of ATS will be disabled while you continue to use an ad-blocker.

 

IOException Java

page: 1
2

log in

join
share:

posted on Mar, 12 2023 @ 04:33 PM
link   
I'm having a difficulty with my code. When I use readObject in my code, I receive an IOException. The application as a whole works OK, but when I try to use readObject, I get an exception; here is the code I use to save objects:


File f = new File("employees.obj");
ObjectOutputStream objOut = null;

try [

objOut = new ObjectOutputStream(new BufferedOutputStream(
new FileOutputStream(f)));
objOut.writeObject(newEmployee);
objOut.flush();

System.out.println("Object is serialized.");

] catch (FileNotFoundException e) [
System.out.println("File not found!");

] catch (IOException e) [
System.out.println("Failed!");

] finally [

if (objOut != null) [
try [

objOut.close();
] catch (IOException e) [
]
]
]


and here is the code I use to restore the object:


File f = new File("employees.obj");
ObjectInputStream objIn = null;
ArrayList c = new ArrayList();
try [
objIn = new ObjectInputStream(new BufferedInputStream(
new FileInputStream(f)));
while (objIn.readObject() != null) [
Person employee = (Person) objIn.readObject();
System.out.println("hello");
System.out.println(employee.toString());
]
System.out.println(c.toString());
return c;

] catch (FileNotFoundException e) [
System.out.println("1");
] catch (IOException e) [
e.printStackTrace();
] catch (ClassNotFoundException e) [
System.out.println("3");
] catch (ClassCastException e) [
System.out.println("4");
] finally [

if (objIn != null) [
try [
objIn.close();
] catch (IOException e) [
System.out.println("4");
]
]
]
return c;


and the outcome in the console:


at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2553)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1296)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at org.bihe.DeSerializer.deSerializeEmployees(DeSerializer.java:20)
at org.bihe.Main.enterAsManager(Main.java:238)
at org.bihe.Main.menu(Main.java:92)
at org.bihe.Main.main(Main.java:50)



posted on Mar, 12 2023 @ 06:29 PM
link   
i use chatgpt every day now for this kind of stuff. From chatgpt, says that the loop condition objIn.readObject() != null is causing the Exception.

Recommends trying a try-catch block to handle the exception. In the catch block, you can break out of the loop.

Says to modify your code like so:

try [
objIn = new ObjectInputStream(new BufferedInputStream(
new FileInputStream(f)));
while (true) [
try [
Person employee = (Person) objIn.readObject();
System.out.println(employee.toString());
c.add(employee);
] catch (EOFException e) [
// End of file reached
break;
]
]
System.out.println(c.toString());
return c;
] catch (FileNotFoundException e) [
System.out.println("File not found!");
] catch (IOException e) [
e.printStackTrace();
] catch (ClassNotFoundException e) [
System.out.println("Class not found!");
] catch (ClassCastException e) [
System.out.println("Class cast exception!");
] finally [
if (objIn != null) [
try [
objIn.close();
] catch (IOException e) [
System.out.println("Error closing file!");
]
]
]


hope this works!



posted on Mar, 13 2023 @ 08:42 AM
link   
yes i too but sometimes it gives wrong and complicated solutions so kinda need human touch
edit on 13-3-2023 by Mobo01 because: (no reason given)



 
2

log in

join