-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectSerialize.java
More file actions
86 lines (72 loc) · 2.54 KB
/
objectSerialize.java
File metadata and controls
86 lines (72 loc) · 2.54 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package emailClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
public class objectSerialize extends emailClient {
private ArrayList<mail> Read_arr;
private ArrayList<mail> existing_arr = new ArrayList<mail>();
private String fileName;
public objectSerialize(String date) throws FileNotFoundException, ClassNotFoundException, IOException {
fileName = date+".ser";
//readFile();
//fileName=date;
}
public objectSerialize() {
}
public void writeToFile() throws FileNotFoundException, IOException, ClassNotFoundException {
createFileName(); //create file name according to the date
File f = new File(fileName);
if(f.exists()) {
fileExist(fileName);
}else {
ObjectOutputStream objout =new ObjectOutputStream(new FileOutputStream(f));
objout.writeObject(arr);
}
}
public void readFile() throws FileNotFoundException, IOException, ClassNotFoundException {
try {
File file = new File(fileName);
//System.out.println(fileName);
if(file.exists()) {
ObjectInputStream objin =new ObjectInputStream(new FileInputStream(file));
Read_arr =(ArrayList<mail>)objin.readObject();
//System.out.println(Read_arr.size());
if(Read_arr.size()==0) {
System.out.println("No mails sent on that date");
}
for(mail Mail:Read_arr) {
System.out.println(Mail.getEmailaddress());
System.out.println(Mail.getSubject());
System.out.println(Mail.getContent());
System.out.println();
}
}else {
System.out.println("file does not exist/ wrong input format");
}
}catch(Exception e) {
System.out.println("wrong input format");
}
}
public void createFileName() {
LocalDate date = LocalDate.now();
String today = date.toString();
fileName = today+".ser"; //2022-08-08.ser
}
public void fileExist(String fileName) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream objin =new ObjectInputStream(new FileInputStream(fileName));
Read_arr =(ArrayList<mail>)objin.readObject();
for(mail Mail:arr) {
Read_arr.add(Mail); // add the existing array into Read_arr
}
ObjectOutputStream objout =new ObjectOutputStream(new FileOutputStream(fileName));
objout.writeObject(Read_arr);
}
}