-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialClass.java
More file actions
66 lines (50 loc) · 2.13 KB
/
SerialClass.java
File metadata and controls
66 lines (50 loc) · 2.13 KB
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
import java.io.*;
import java.util.ArrayList;
public class SerialClass {
// Serialize today's date to a file.
public static void main(String[] args) throws Exception {
/*FileOutputStream f = new FileOutputStream("tmp");
ObjectOutput s = new ObjectOutputStream(f);
//s.writeObject("Today");
s.writeObject(new Date("July", 29));
s.flush();*/
SerialClass serial = new SerialClass();
/*Date writeDate1 = new Date("Dec", 25);
writeDate1.setHoliday(new Holiday("Crimas"));
ArrayList<Birthday> crimasBdays = new ArrayList<>();
crimasBdays.add(new Birthday("jesus", 2022));
crimasBdays.add(new Birthday("john", 22));
writeDate1.setBirthdaysToday(crimasBdays);
serial.WriteObjectToFile("tmp9", writeDate1);*/
Date readDate1 = (Date) serial.ReadObjectFromFile("tmp9");
readDate1.getBirthdaysToday().add(new Birthday("greg", 20));
//Date currDate = new Date("Brown", 29);
System.out.println(readDate1);
//Date date = (Date) serial.WriteObjectToFile("tmp");
//System.out.println(date);
}
public void WriteObjectToFile(String filepath,Object serObj) {
try {
FileOutputStream fileOut = new FileOutputStream(filepath);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(serObj);
objectOut.close();
System.out.println("The Object was succesfully written to a file");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Object ReadObjectFromFile(String filepath) {
try {
FileInputStream fileIn = new FileInputStream(filepath);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
Object obj = objectIn.readObject();
System.out.println("The Object has been read from the file");
objectIn.close();
return obj;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}