-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileReader.java
64 lines (63 loc) · 1.9 KB
/
FileReader.java
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
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class FileReader
{
private static SimpleDateFormat df = new SimpleDateFormat();
//create or overwrite a file, given its name, for storing todoItems
public static void storeData(TodoList list, String filename) throws IOException
{
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
Iterator<TodoItem> iter = list.iterator();
TodoItem temp;
while(iter.hasNext())
{
temp = iter.next();
writer.write(temp.getTitle());
writer.newLine();
writer.write(temp.getDescription());
writer.newLine();
writer.write(temp.getCompleted()+"");
writer.newLine();
if(temp.getCalendar() != null){
writer.write(df.format(temp.getCalendar().getTime()));
}
writer.newLine();
writer.newLine();
}
writer.close();
}
//return a list of todoItems given a file providing information
public static TodoList readData(String filename) throws IOException
{
TodoList list = new TodoList();
Scanner scan = new Scanner(new File(filename));
TodoItem temp;
while(scan.hasNextLine())
{
temp = new TodoItem();
temp.setTitle(scan.nextLine());
temp.setDescription(scan.nextLine());
temp.setCompleted(Boolean.parseBoolean(scan.nextLine()));
Calendar cal = Calendar.getInstance();
String stringDate = "";
if(scan.hasNextLine())
stringDate = scan.nextLine();
try{
if(stringDate.equals("")){
cal = null;
}else{
cal.setTime(FileReader.df.parse(stringDate));
}
}catch(ParseException e){
e.printStackTrace();
}
temp.setCalendar(cal);
list.add(temp);
if(scan.hasNextLine())
scan.nextLine(); //advance to the next line, tasks are seperated by empty lines
}
return list;
}
}