Repository navigation
Expand file tree
/
Copy pathLineCounter.java
More file actions
329 lines (276 loc) · 7.23 KB
/
Copy pathLineCounter.java
File metadata and controls
329 lines (276 loc) · 7.23 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/**
* @author Matt Henry
*
* This program is to be run with a txt or csv file name passed in as a
* parameter. The file will be opened, the number of lines will be
* counted, the file will be closed, and a brief report about the file
* including number of lines contained in the file will then be printed
* to the console. Only files of type csv and txt will be accepted. All
* file new line indicators are assumed to be a carriage return followed
* by a line feed (DOS/Windows format).
*
*/
public class LineCounter {
Scanner scanner; // for txt reading
private String fileName;
private String fileType;
private int numLines;
/**
* @param args
*/
public static void main(String[] args) {
// make sure argument has been provided
if (args.length == 0) {
System.out
.println("Usage: LineCounter filename");
System.exit(0);
}
LineCounter lineCounter = new LineCounter(args[0]);
// validate file
if (!lineCounter.validateFile()) {
System.out.println("Invalid file type. Must be txt or csv file.");
System.exit(0);
}
// count lines in file
if (lineCounter.getFileType().equals("txt")) {
lineCounter.countTxt();
} else {
lineCounter.countCsv();
}
// print report
lineCounter.printReport();
}
/**
* @param fileName
*
* Constructor
*
*/
LineCounter(String fileName) {
this.fileName = fileName;
}
/**
* @param fileName
* @return
*
* validates the file name as txt/csv and returns true if valid
*/
boolean validateFile() {
if (fileName.length() < 5) {
return false;
}
String fileExtension = fileName.substring(fileName.length() - 4, fileName.length());
if (fileExtension.equals(".txt") || fileExtension.equals(".csv")) {
fileType = fileName.substring(fileName.length() - 3, fileName.length());
return true;
}
return false;
}
/**
* counts number of lines in txt file
*/
private void countTxt() {
try {
System.out.println("Opening " + fileName);
scanner = new Scanner(new FileReader(fileName));
} catch (FileNotFoundException e) {
System.out.println("File not found. Please make sure file exists.");
System.exit(0);
}
while (scanner.hasNextLine()) {
scanner.nextLine();
numLines++;
}
scanner.close();
}
/**
* countCsv counts number of rows contained in a csv file. csv file is assumed to be
* a valid csv file as specified in RFC 4180 -
* http://tools.ietf.org/html/rfc4180
*
* The logic in this method will increment numLines if the new line (CR LF)
* combination is found not in a 'protected' section, and is not directly
* followed by the end of the file. A protected section is defined by being
* contained in quotes. As specified by RFC4180, any record in the csv
* containing a new line char(CR LF), comma(44), or double quote(34) must be
* contained in double quotes. In addition, and double quotes contained in a
* record must be escaped by a preceding double quote.
*
* I've designed the logic in this method using a state machine. The states
* are defined as:
*
* State 1 - Non Protected(or protected section is past). Consume all chars
* except:
* CR - go to state 5
* EOF - end
* " = go to state 2
* State 2 - Begin protected record. Consume all chars except:
* " - go to state 3
* State 3 - Saw a double quote inside of a protected section. Indicates
* protected section is now over, or this double quote will preceed
* (escape) another double quote. Consume all chars except:
* " - go to state 4
* EOF - end
* , - go to state 1
* State 4 - Presence of a double quote directly after another double quote while in
* a protected section. This quote has been escaped by the
* preceeding quote. Consume all chars except:
* " - go to state 3
* State 5 - CR present outside of protected section. Go to stage 1 for all
* chars except:
* LF - go to state 6
* State 6 - New line indicator (CR LF) present. Increment line count and go to
* state 1 for all chars except:
* " - increment line count, go to state 2
* EOF - end
*/
private void countCsv() {
BufferedReader br = null;
int chr;
int state = 0;
boolean running = false;
try {
br = new BufferedReader(new FileReader(fileName));
running = true;
} catch (FileNotFoundException e) {
System.out.println("File not found. Please make sure file exists.");
System.exit(0);
}
while (running) {
try {
// read each character
chr = br.read();
if (state == 0) { // beginning state
switch (chr) {
case -1: { // if file is empty it goes to state 7, and no
// lines are counted.
running = false;
break;
}
case 34: { // if quote is first char, goes to state 2,
// account for first line
state = 2;
numLines++;
break;
}
default: { // else consume chars, account for first line
state = 1;
numLines++;
break;
}
}
} else if (state == 1) {
switch (chr) {
case 13: { // CR
state = 5;
break;
}
case -1: { // EOF
running = false;
break;
}
case 34: { // "
state = 2;
break;
}
default: {
state = 1;
break;
}
}
} else if (state == 2) {
switch (chr) {
case 34: { // "
state = 3;
break;
}
default: { // consume
state = 2;
break;
}
}
} else if (state == 3) {
switch (chr) {
case -1: { // EOF
running = false;
break;
}
case 34: { // "
state = 4;
break;
}
case 13: { // CR
state = 5;
break;
}
case 44: { // ,
state = 1;
break;
}
}
} else if (state == 4) {
switch (chr) {
case 34: { // "
state = 3;
break;
}
default: {
state = 4;
break;
}
}
} else if (state == 5) {
switch (chr) {
case 10: { // LF
state = 6;
break;
}
default: {
state = 1;
break;
}
}
} else if (state == 6) {
switch (chr) {
case -1: { // EOF
running = false;
break;
}
case 34: { // "
state = 2;
numLines++;
break;
}
default: {
state = 1;
numLines++;
break;
}
}
}
} catch (IOException e) {
System.out.println("Unable to read file.");
}
}
try {
br.close();
} catch (IOException e) {
System.out.println("Unable to close file.");
}
}
/**
* print brief summary
*/
private void printReport() {
System.out.println("File name: " + fileName + ".");
System.out.println("Total lines: " + numLines + ".");
}
public String getFileType() {
return fileType;
}
}