-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntryList.java
More file actions
163 lines (143 loc) · 4.32 KB
/
EntryList.java
File metadata and controls
163 lines (143 loc) · 4.32 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.util.*;
import java.io.*;
/** @author Gergely Kota
EntryList provides an interface for reading and writing from
the departments.txt file by keys and values.
*/
public class EntryList
{
private static HashMap strings;
private static ArrayList depts;
/** INPUT is the File from which options are read */
public static final File INPUT = new File("data/departments.txt");
/** ESCAPE is the sequence that comments the line */
public static final String ESCAPE = "//";
/** DELIM is the sequence that separates dept and url */
public static final String DELIM = "==";
/** ILLEGAL is the return for a non-existent int */
public static final int ILLEGAL = Integer.MIN_VALUE;
// read in key/value pairs on startup
static
{
init();
}
// init reads the config file and sets up all key/value pairs
// in a HashMap. NOTE: All keys are mapped to lowercase for
// case insensitivity
private static void init()
{
strings = new HashMap();
depts = new ArrayList();
try
{
// create a reader
BufferedReader br = new BufferedReader(new FileReader(INPUT));
while(br.ready())
{
// try each line of the config file
try
{
// read a line
// check if it starts with ESCAPE
// pull key off the front
// the rest is the value
// add value hashed by key
String s = br.readLine().trim();
if(s.startsWith(ESCAPE))
continue;
String dept = s.substring(0, s.indexOf(DELIM)).trim();
String url = s.substring(s.indexOf(DELIM)+DELIM.length()).trim();
strings.put(dept.toLowerCase(), url);
depts.add(dept);
}
// if there are any problems, ignore this line
catch(Exception e) {}
}
}
// failed reader results in no key/value pairs.
catch(Exception e) {}
Collections.sort(depts);
}
/** @param key the String by which the lookup occurs
@param value the String to associate with the key
@return true if the write succeeded
write allows a program to add or change information in the
config file. If the key is already used, the associated value
is replaced. If the key is new, the new key/value pair is added
to the end of the config file. In either case, the config file
is then reread to reflect the changes immediately.
*/
public static boolean write(String dept, String url)
{
if(dept == null || url == null)
return false;
boolean worked = true;
boolean replaced = false;
StringBuffer sb = new StringBuffer();
try
{
BufferedReader br = new BufferedReader(new FileReader(INPUT));
while(br.ready())
{
// read one line at a time
// for each, check if it uses the passed key
// if so, replace its value, else keep it as is
String temp = br.readLine();
if(temp.toLowerCase().trim().startsWith(dept.toLowerCase()))
{
temp = dept.trim() + " " + DELIM + " " + url.trim();
replaced = true;
}
sb.append(temp).append("\n");
}
if(!replaced)
sb.append(dept).append(" ").append(DELIM).append(" ").append(url).append("\n");
}
// make a note in case of any failure
catch(Exception e) {worked = false;}
// sb now holds the new contents of the file.
// if there was a failure, do not write the file (to avoid corruption or loss)
if(!worked)
return false;
try
{
// write the contents of the new file to the config file
// reread the contents of the file to reflect the changes
PrintWriter pw = new PrintWriter(new FileWriter(INPUT));
pw.print(sb.toString());
pw.close();
init();
return true;
}
catch(Exception e) {return false;}
}
// no instances exist.
private EntryList() {}
/** @param the key to look up a value for
@return the value associated with that key
read a value associated with a key from the config file
*/
public static String read(String dept)
{
if(strings == null)
return null;
return (String) strings.get(dept.toLowerCase());
}
public static String[] entries()
{
Object[] o = depts.toArray();
String[] s = new String[o.length];
for(int i = 0; i < s.length; i++)
s[i] = (String) o[i];
return s;
}
/* -------------------------------------------- */
/* -------------------------------------------- */
/* -------------------------------------------- */
public static void main(String[] ahrs)
{
System.out.println(read("Arizona"));
System.out.println(write("asu", "www.cs.asu.edu"));
System.out.println(read("ASU"));
}
}