Reputation: 261
For some odd reason I am able to write my object to a file if broken up into variables or Strings but not as shown in the tutorial as one solid Object.
EXAMPLE SUCCESS:
public Book add(Book book){
try{
FileOutputStream fos = new FileOutputStream("stream.txt");
ObjectOutputStream output = new ObjectOutputStream(fos);
output.writeObject(book.getString1());
output.writeObject(book.getString2());
output.writeObject(book.getString3());
output.flush();
output.close();
}catch(Exception e){
System.out.print("Falure to write!");
}
return book;
}
While the following code completely bombs out and jumps to catch as soon as I attempt to write the Object:
EXAMPLE FAIL:
public Book add(Book book){
try{
FileOutputStream fos = new FileOutputStream("stream.txt");
ObjectOutputStream output = new ObjectOutputStream(fos);
output.writeObject(book);
output.flush();
output.close();
}catch(Exception e){
System.out.print("Falure to write!");
}
return book;
}
Upvotes: 0
Views: 901
Reputation: 16037
The Book
class should implement Serializable interface
Also, what was the error message?
}catch(Exception e){
System.out.println(e);
}
Upvotes: 1