-
-
Save belun/2214237 to your computer and use it in GitHub Desktop.
BinaryTree saved/loaded from Text File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package binaryTree; | |
import java.io.Serializable; | |
public class Leaf implements Visualizable, Serializable { | |
private int value; | |
public Leaf(int value) { | |
this.value = value; | |
} | |
@Override | |
public void show() { | |
System.out.println("<Leaf>"); | |
System.out.println("<Value>" + value + "</Value>"); | |
System.out.println("</Leaf>"); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package binaryTree; | |
import java.io.Serializable; | |
public class Node implements Visualizable, Serializable { | |
private Leaf value; | |
private Visualizable leftSide; | |
private Visualizable rightSide; | |
public Node(int value, Visualizable leftSide, Visualizable rightSide) { | |
this.value = new Leaf(value); | |
this.leftSide = leftSide; | |
this.rightSide = rightSide; | |
} | |
@Override | |
public void show() { | |
System.out.println("<Node>"); | |
value.show(); | |
leftSide.show(); | |
System.out.println("</LeftSide>"); | |
System.out.println("<RightSide>"); | |
rightSide.show(); | |
System.out.println("</RightSide>"); | |
System.out.println("</Node>"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package marshalling; | |
import binaryTree.Visualizable; | |
import org.jboss.marshalling.*; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
// Stuff mostly taken from an example of the JBoss API | |
public class Reader { | |
public Reader() { | |
} | |
public Visualizable readFrom(String fileName) { | |
Object sourceObject = null; | |
MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("river"); | |
MarshallingConfiguration configuration = new MarshallingConfiguration(); | |
try { | |
FileInputStream input = new FileInputStream(fileName); | |
try { | |
Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration); | |
ByteInput byteInput = Marshalling.createByteInput(input); | |
unmarshaller.start(byteInput); | |
sourceObject = unmarshaller.readObject(); | |
unmarshaller.finish(); | |
byteInput.close(); | |
} catch (IOException e) { | |
System.out.print(" ERROR: Could not load from file"); | |
} catch (ClassNotFoundException exception) { | |
System.out.print(" ERROR: Could not load from file"); | |
} finally { | |
try { | |
input.close(); | |
} catch (IOException e) { | |
} | |
} | |
} catch (FileNotFoundException exception) { | |
System.out.print(" ERROR: Could not load from file"); | |
} | |
return (Visualizable) sourceObject; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import binaryTree.Leaf; | |
import binaryTree.Node; | |
import binaryTree.Visualizable; | |
import marshalling.Reader; | |
import marshalling.Writer; | |
public class TreeCreator { | |
public static void main(String[] arguments) { | |
Visualizable binaryTree = new Node(3, new Node(4, new Leaf(6), new Leaf(5)), new Node(7, new Node(8, new Leaf(9), new Leaf(10)), new Leaf(11))); | |
binaryTree.show(); | |
// using jboss-marshalling 1.3.0 and JBoss Marshalling Serial Protocol 1.3.0 from http://www.jboss.org/jbossmarshalling/downloads | |
new Writer(binaryTree).putInto("BinaryTreeMarshaled.txt"); | |
Visualizable unmarshalledBinaryTree = new Reader().readFrom("BinaryTreeMarshaled.txt"); | |
unmarshalledBinaryTree.show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package binaryTree; | |
public interface Visualizable { | |
void show(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package marshalling; | |
import binaryTree.Visualizable; | |
import org.jboss.marshalling.*; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
// Stuff mostly taken from an example of the JBoss API | |
public class Writer { | |
private Visualizable binaryTree; | |
public Writer(Visualizable binaryTree) { | |
this.binaryTree = binaryTree; | |
} | |
public void putInto(String fileName) { | |
MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("river"); | |
MarshallingConfiguration configuration = new MarshallingConfiguration(); | |
configuration.setVersion(3); | |
try { | |
FileOutputStream output = new FileOutputStream(fileName); | |
try { | |
Marshaller marshaller = marshallerFactory.createMarshaller(configuration); | |
ByteOutput byteOutput = Marshalling.createByteOutput(output); | |
marshaller.start(byteOutput); | |
marshaller.writeObject(binaryTree); | |
marshaller.finish(); | |
output.close(); | |
} catch (IOException exception) { | |
System.out.print(" ERROR: Could not put into file"); | |
} finally { | |
try { | |
output.close(); | |
} catch (IOException exception) { | |
} | |
} | |
} catch (FileNotFoundException exception) { | |
System.out.print(" ERROR: Could not put into file"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment