-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileReaders.java
146 lines (137 loc) · 5.12 KB
/
FileReaders.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
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
public class FileReaders {
/**
* This method reads a file and stores some of its contents (lineID, lineCode) in a Hash Map.
*
* @param fileName File used as an input to read from.
* @return A Hash Map containing LineID & LineCode.
*/
public static HashMap<String, String> readBusLines(File fileName) {
HashMap<String, String> busLines = new HashMap<>();
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(sCurrentLine, ",");
String lineCode = st.nextToken();
String lineID = st.nextToken();
busLines.put(lineCode, lineID);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
if (fr != null) fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return busLines;
}
/**
* This method reads a file and stores some of its contents (lineID, lineCode) in a Hash Map.
*
* @param fileName File used as an input to read from.
* @return A Hash Map containing LineID & LineCode.
*/
public static HashMap<String, String> readHash(File fileName) {
HashMap<String, String> busLines = new HashMap<>();
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(sCurrentLine, ",");
String lineCode = st.nextToken();
String lineID = st.nextToken();
busLines.put(lineID, lineCode);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
if (fr != null) fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return busLines;
}
public static HashMap<String, String> readRoutCodes(File fileName) {
HashMap<String, String> routCodes = new HashMap<>();
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(sCurrentLine, ",");
String RouteCode = st.nextToken();
st.nextToken();
st.nextToken();
String Description = st.nextToken();
routCodes.put(Description,RouteCode);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
if (fr != null) fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return routCodes;
}
/**
* This method reads a file and stores some of its contents (lineCode, latitude, longitude) in an Array List.
*
* @param fileName File used as an input to read from
* @return An Array List containing LineCode & Latitude-Longitude.
*/
public static ArrayList<Message> readBusPositions(File fileName) {
ArrayList<Message> busPositions = new ArrayList<>();
BufferedReader br = null;
FileReader fr = null;
String latitude, longitude;
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(sCurrentLine, ",");
String lineCode = st.nextToken();
String RouteCode = st.nextToken();
String VehicleId =st.nextToken();
latitude = st.nextToken();
longitude = st.nextToken();
busPositions.add(new Message("NoTopicMsg", lineCode ,RouteCode,VehicleId,latitude + ", " + longitude));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
if (fr != null) fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return busPositions;
}
}