This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Getting error while serializing. Please see the following code while produces IOException.  RSS feed

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package IOFiles;
import java.io.*;
public class TestSerialize2 {

public static void main(String[] args) {

OrderCoffee ordcof = new OrderCoffee(new Coffee(2),3);
System.out.println("Before Serialize::: ");
ordcof.getCoffee();
try{
FileOutputStream fo = new FileOutputStream("mytestser2.ser");
ObjectOutputStream oo = new ObjectOutputStream(fo);
oo.writeObject(ordcof);
oo.close();
}
catch(Exception e){
e.printStackTrace();
}

try{
FileInputStream fi = new FileInputStream("mytestser2.ser");
ObjectInputStream oi = new ObjectInputStream(fi);
OrderCoffee c2=(OrderCoffee)oi.readObject();
oi.close();
System.out.println("After Serialize::: ");
c2.getCoffee();
}
catch(Exception e){
e.printStackTrace();
}
}

}
class OrderCoffee implements Serializable{
private transient Coffee coffee;
private int size;
OrderCoffee(Coffee c, int size){
coffee = c;
this.size = size;
}
public void getCoffee(){
System.out.println("The coffee you requested is "+coffee.getSize());
}

private void writeObject(ObjectOutputStream os){
try{
os.defaultWriteObject();
os.writeInt(size);
os.close();
}catch(Exception e ){ }
}

private void readObject(ObjectInputStream oi){
try{
oi.defaultReadObject();
coffee=new Coffee(oi.readInt());
oi.close();
}catch(Exception e ){ }
}
}

class Coffee //implements Serializable
{
private int size;
Coffee(int size){
this.size=size;
}
public String getSize(){
String s;
if(size==1) s="small";
else if(size==2) s="medium";
else if(size==3) s="large";
else s="not available";
return s;

}
}

For the program above, iam getting the following output as below,

java.io.IOException: Write error
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at IOFiles.TestSerialize2.main(TestSerialize2.java:13)
java.io.IOException: Read error
at java.io.FileInputStream.read(Native Method)
at java.io.ObjectInputStream$PeekInputStream.peek(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.peek(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.skipCustomData(Unknown Source)Before Serialize:::
The coffee you requested is medium

at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at IOFiles.TestSerialize2.main(TestSerialize2.java:23)


can anybody tell me why iam getting exception? I have overriddent the private void writeObject(ObjectOutputSteam os) &
private void readObject(ObjectInputStream oi) to write the state of the 'size' variable in the Coffee class. and put the 'size' in the Coffee using, coffee=new Coffee(oi.readInt());

I dont understand why iam not getting the "The coffee you requested is large" after serialization. as used value of '3' in OrderCoffee constructor and saved the size, used that size, ie. size =3 to invoke the Coffee(int size) constructor in private void readObject(ObjectInputStream oi) constructor.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please write your code using code tags.
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[/code]
} [/code]

For the program above, iam getting the following output as below,

java.io.IOException: Write error
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at IOFiles.TestSerialize2.main(TestSerialize2.java:13)
java.io.IOException: Read error
at java.io.FileInputStream.read(Native Method)
at java.io.ObjectInputStream$PeekInputStream.peek(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.peek(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.skipCustomData(Unknown Source)Before Serialize:::
The coffee you requested is medium

at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at IOFiles.TestSerialize2.main(TestSerialize2.java:23)

can anybody tell me why iam getting exception? I have overriddent the private void writeObject(ObjectOutputSteam os) &
private void readObject(ObjectInputStream oi) to write the state of the 'size' variable in the Coffee class. and put the 'size' in the Coffee using, coffee=new Coffee(oi.readInt());

I dont understand why iam not getting the "The coffee you requested is large" after serialization. as used value of '3' in OrderCoffee constructor and saved the size, used that size, ie. size =3 to invoke the Coffee(int size) constructor in private void readObject(ObjectInputStream oi) constructor.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its the same problem you have in the other code you posted.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you must not close write or read streams in writeObject or readObject methods.
 
Run away! Run away! Here, take this tiny ad with you:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!