Somebody please help me with this code .
i am implementing readObject()/writeObject().
Trying to reading a
String in ObjectInputStream. I tried different ObjectInputStream methods
readLine()->depracated ,Gives my o/p string along wih cryptic characters.
readLine() reads UTF so doesnt work,since i am readng plain file
readObect()->then casting to String gives exception .
import java.io.*;
/// non serializable class ,Instance as transient in Plant class
//---------------giving default values by using custom-serialization--
class Flower {
String flower;
Flower() {
System.out.println("i am flower");
flower = "dalia";
}
Flower(String s) {
flower = s;
}
String getFlowerValue() // putting some default values for transient class
// instance
{
return flower;
}
}
// ---------------------serializable class ,Instance(not transient) in Plant
// class
class Fruit1 implements Serializable {
String
fruit;
Fruit1() {
System.out.println("i am fruit");
fruit = "mango";
}
}
// /-------------Serializable class------------- using objects of Fruit &
// Flower(transient)//
class Plant implements Serializable {
transient Flower myFlower; // /non serializable
Fruit1 myFruit;
Plant() {
System.out.println("Constructor:i am a plant wih flower & veg");
myFlower = new Flower();
myFruit = new Fruit1();
}
// custom serialization ----------------------
// implmenting methods --to write default Flower values with plant object
private void writeObject(ObjectOutputStream oos) {
try {
oos.defaultWriteObject();
System.out
.println("Explicitly writing Default Flower Object-Values: "
+ myFlower.getFlowerValue());
oos.writeChars(myFlower.getFlowerValue());
} catch (IOException e) {
System.out.println("Error in writing exolicitly");
}
}
// implementing readObject()
// Explicitly reading Flower
private void readObject(ObjectInputStream ois) {
System.out.println("----------------Here is BUG--------------");
try {
ois.defaultReadObject(); // normal object reading
String s = (String) ois.readObject();
// String-reading ,falls to IOException
// readLine() gives o/p but not proper,readUTF() -doesnt work
System.out.println("Checking String:->" + s);
myFlower = new Flower(s);
System.out.println(myFlower.flower);
ois.close();
} catch (ClassNotFoundException e) {
System.out.println("class not foud " + e);
}
catch (IOException e) {
System.out.println("IO inside readObject() Error " + e);
e.printStackTrace();
}
}
}
// /------------------------main class------------serializing................
public class SerialDemo4 {
public static void main(String args[]) {
Plant saved_plant = new Plant();
System.out.println("Plant object created ");
try {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("Plants.ser"));
oos.writeObject(saved_plant);
oos.close();
System.out.println("Plant objects written with Fruit ");
System.out
.println("------------------------------------------------------------------");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"Plants.ser"));
Object obj = ois.readObject();
Plant recov_plant = (Plant) obj;
// Plant plant1=(Plant)ois.readObject();
ois.close();
System.out.println("objects recovered-> " + recov_plant + " "
+ recov_plant.myFlower.flower + " "
+ recov_plant.myFruit.fruit);
// myFlower(transient) --doesnt work, myFruit-workss
} catch (IOException e) {
System.out.println("Main Program:IO error " + e);
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("Main Program:Class not found while reading "
+ e);
}
}
}