summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/prjava/src/io/SerializedObjects.java
blob: d1631b5159671ed7f0257cceb4a75db583066e92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package io;



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class SerializedObjects
{
	public static void writeSerializedObject(Object object, String outFile)
	{
		ObjectOutput output = null;
	    try{
	      //use buffering
	      OutputStream file = new FileOutputStream(outFile);
	      OutputStream buffer = new BufferedOutputStream( file );
	      output = new ObjectOutputStream( buffer );
	      output.writeObject(object);
	      buffer.close();
	      file.close();
	    }
	    catch(IOException ex){
	    	ex.printStackTrace();
	    }
	    finally{
	      try {
	        if (output != null) {
	          //flush and close "output" and its underlying streams
	          output.close();
	        }
	      }
	      catch (IOException ex ){
	    	  ex.printStackTrace();
	      }
	    }
	}
	
	public static Object readSerializedObject(String inputFile)
	{
		ObjectInput input = null;
	    Object recoveredObject=null;
		try{
	      //use buffering
	      InputStream file = new FileInputStream(inputFile);
	      InputStream buffer = new BufferedInputStream(file);
	      input = new ObjectInputStream(buffer);
	      //deserialize the List
	      recoveredObject = input.readObject();
	    }
	    catch(IOException ex){
	    	ex.printStackTrace();
	    }
	    catch (ClassNotFoundException ex){
	    	ex.printStackTrace();
	    }
	    catch(Exception ex)
	    {
	    	ex.printStackTrace();
	    }
	    finally{
	      try {
	        if ( input != null ) {
	          //close "input" and its underlying streams
	          input.close();
	        }
	      }
	      catch (IOException ex){
	    	  ex.printStackTrace();
	      }
	    }
	    return recoveredObject;
	 }
	
}