Using java.io.Serializable when implementing a tree?
I have a question about ANOTHER serialization, but this time it is about importing native Java serialization when serializing to binary. I have to serialize a random tree that is generated in another java file. I know how serialization and deserialization works, but the example I used when using binary serialization with java.io.Serializable didn't work in the same way as when I did, say, a simple object. Here is my code segment:
import java.io.*;
import java.io.FileInputStream;
public class BinaryS
{
public static void main(String[] args) {
Tree randomTree = RandomTreeBuilder.randomTree(10);
FileOutputStream fOut=null;
ObjectOutputStream oOut=null;
try{
fOut= new FileOutputStream("/Users/Pat/programs/binaryfile.txt");
oOut = new ObjectOutputStream(fOut);
oOut.writeObject(randomTree); //serializing randomTree
System.out.println("An employee is serialized into /Users/Pat/binaryfile.txt");
}catch(IOException e){
e.printStackTrace();
}finally{
try {
oOut.flush();
oOut.close();
fOut.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
I believe the problem is that I am using writeObject (randomTree). I am getting some terminal exceptions when this happens ... they are below:
java.io.NotSerializableException: GeneralTree at java.io.ObjectOutputStream.writeObject0 (ObjectOutputStream.java:1081) at java.io.ObjectOutputStream.writeObject (ObjectOutputStream.java:302) at BinarySjain (BinarySav24)
edit: I know what it says is GeneralTree, but at the beginning of the class it was in I put
print("public class RandomTreeBuilder implements java.io.Serializable");
then GeneralTree is displayed under it
print(" protected static Tree tree;
protected static ArrayList names;
//e6.1
/**
*Builds a random tree. The build method does the work.
*/
//b6.2
public static Tree randomTree(int n) {
// Create a random binary tree with n external nodes
tree = new GeneralTree();
names = NameGenerator.getNames();
build(tree.getRoot(), n); // auxiliary recursive method
return tree;
");
Update: Hey guys I figured out my own problem, it turns out I'm an idiot and didn't realize I needed to download an additional .java file, easy fix! Thank you for your help!
source to share
Assuming GeneralTree does not implement the Serializable marker interface as described here .
Actually, it can also be objects that you store in the tree that are not Serializable. A collection can be serialized if all the elements in it are also.
source to share
edit: I know what GeneralTree says, but at the beginning of the class it was in I put
print("public class RandomTreeBuilder implements java.io.Serializable");
This is not good for you โ you are not trying to write an object of type RandomTreeBuilder to an object stream; the type you are trying to write is GeneralTree and this is the class that Serializable should implement.
FYI: In case you haven't come across this, this article covers many of the serialization tricks and caveats: Discover the secrets of the Java Serialization API .
source to share