source: src/main/java/genius/core/SerializeHandling.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 1.7 KB
Line 
1package genius.core;
2
3import java.io.FileInputStream;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.ObjectInputStream;
7import java.io.ObjectOutputStream;
8import java.io.Serializable;
9
10/**
11 * This is a utility class to handle writing and
12 * reading {@link Serializable} objects into/from a file.
13 * @author Samanta H.
14 *
15 */
16public class SerializeHandling{
17
18 /**
19 * Writes the data into disc, in a new file under the path given.
20 * @param data the object to be saved into a new file.
21 * @param path the final path (including the name of the file which will
22 * be created).
23 * @return true if writing the object into the path was successful.
24 * false otherwise.
25 */
26 public static boolean writeToDisc(Serializable data, String path){
27 try{
28
29 FileOutputStream fout = new FileOutputStream(path);
30 ObjectOutputStream oos = new ObjectOutputStream(fout);
31 oos.writeObject(data);
32 oos.close();
33// System.out.println("Done writing.");
34
35 }catch(Exception ex){
36 ex.printStackTrace();
37 return false;
38 }
39 return true;
40 }
41
42 /**
43 * Reads an Object from the "path" in the disc.
44 * @param path the path where the object is saved in the disc.
45 * @return the object saved in path.
46 */
47 public static Serializable readFromDisc(String path) {
48 Serializable data;
49 try{
50
51 FileInputStream fin = new FileInputStream(path);
52 ObjectInputStream ois = new ObjectInputStream(fin);
53 data = (Serializable) ois.readObject();
54 ois.close();
55// System.out.println("Done reading.");
56 return data;
57
58 }catch(FileNotFoundException e){
59 return null;
60 }catch(Exception ex){
61 ex.printStackTrace();
62 return null;
63 }
64 }
65}
Note: See TracBrowser for help on using the repository browser.