Advertisement
Guest User

Serialization Failure

a guest
Apr 29th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. public class Start {
  2.     public static void main(String[] args) {
  3.         File aFile = new File("");
  4.         String dataPathBIN = aFile.getAbsolutePath() + "/data/wiprofs.dta";
  5.         String dataPathXML = aFile.getAbsolutePath() + "/data/wiprofs.xml";
  6.          System.out.println("Test binaray serialization");
  7.          new BinarySerialization(dataPathBIN);
  8.          System.out.println();
  9.          System.out.println("Test serialization with xml");
  10.          new XMLSerialization(dataPathXML);
  11.     }
  12. }
  13.  
  14. public class Student implements Serializable{
  15.     private static final long serialVersionUID = 1L;
  16.     private int matrikel = 0;
  17.     private String name = "", eMail = "";
  18.    
  19.     public Student() { 
  20.     }
  21.     public Student(int matrikel, String name, String eMail) {
  22.         this.matrikel = matrikel;
  23.         this.name = name;
  24.         this.eMail = eMail;
  25.     }
  26.  
  27.     public int getMatrikel() { return matrikel;
  28.     }
  29.     public String geteMail() { return eMail;
  30.     }
  31.     public String getName() { return name;
  32.     }
  33.     public void setMatrikel(int matrikel) { this.matrikel = matrikel;
  34.     }
  35.     public void setName(String name) { this.name = name;
  36.     }
  37.     public void seteMail(String eMail) { this.eMail = eMail;
  38.     }
  39. }
  40.  
  41. import java.util.ArrayList;
  42.  
  43.  
  44. public abstract class Serialization {
  45.     protected ArrayList<Student> students = new ArrayList<Student>();
  46.     protected String dataPath = "";
  47.    
  48.     public Serialization(String dataPath) {
  49.         this.dataPath = dataPath;
  50.         }
  51.     protected void createData() {
  52.         students.add(new Student(111111, "Peter Petersen", "petersen@stud.de"));
  53.         students.add(new Student(222222, "Chris Chrisson", "chrisson@stud.de"));
  54.         students.add(new Student(333333, "Karla Karlson", "karlson@stud.de"));
  55.         students.add(new Student(444444, "Rocky Balboa", "balboa@stud.de"));
  56.     }
  57.    
  58.     protected void clearData() {
  59.         students.removeAll(students);
  60.     }
  61.     protected void showData() {
  62.         for(Student student : students) {
  63.             System.out.println(student);
  64.         }
  65.     }
  66.    
  67.     protected abstract void readData();
  68.     protected abstract void writeData();
  69. }
  70.  
  71.  
  72. public class BinarySerialization extends Serialization{
  73.  
  74.     public BinarySerialization(String dataPath) {
  75.         super(dataPath);
  76.         createData();
  77.         writeData();
  78.         clearData();
  79.         readData();
  80.         showData();
  81.     }
  82.     @SuppressWarnings("unchecked")
  83.     @Override
  84.     protected void readData() {
  85.         try {
  86.             FileInputStream in = new FileInputStream(dataPath);
  87.             ObjectInputStream oin = new ObjectInputStream(in);
  88.             students = (ArrayList<Student>)oin.readObject();
  89.             oin.close();
  90.         } catch (Exception e) {
  91.             System.out.println(e);
  92.         }
  93.     }
  94.  
  95.     @Override
  96.     protected void writeData() {
  97.         try {
  98.             FileOutputStream out = new FileOutputStream(dataPath);
  99.             ObjectOutputStream oout = new ObjectOutputStream(out);
  100.             oout.writeObject(students);
  101.             oout.close();
  102.         } catch (Exception e) {
  103.             System.out.println(e);
  104.         }  
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement