-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSVReader.java
92 lines (83 loc) · 1.96 KB
/
CSVReader.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
/**
* CSVReader Class
*/
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.LinkedList;
public class CSVReader {
/**
* BufferedReader
*
* @var java.io.BufferedReader
*/
private BufferedReader Reader;
/**
* Constructor
*
* @param java.io.File Source Input CSV File.
*
* @throws java.io.IOException If unable to read from the Source File.
*
* @return void
*/
public CSVReader(File Source) throws IOException {
try {
Reader = new BufferedReader(new FileReader(Source));
} catch (IOException e) {
throw e;
}
}
/**
* Reads a row of data from the Source CSV File as an Array of String
*
* @throws java.io.IOException If unable to read from Source file.
*
* @return String[]
*/
public String[] readRow() throws IOException {
LinkedList<String> Row = new LinkedList<String>();
try {
String Line = Reader.readLine();
if (Line == null) return null;
boolean Hold = false;
String Value = new String("");
for (int i = 0; i<Line.length(); i++) {
switch (Line.charAt(i)) {
case '"':
Hold = !Hold;
break;
case ',':
if (Hold) {
Value += Line.charAt(i);
} else {
Row.add(Value);
Value = "";
}
break;
default:
Value += Line.charAt(i);
break;
}
}
return Row.toArray(new String[Row.size()]);
} catch (IOException e) {
throw e;
}
}
/**
* Closes the stream and releases any system resources associated with it.
*
* @throws java.io.IOException If unable to read from Source file.
*
* @return void
*/
public void close() throws IOException {
try {
Reader.close();
} catch (IOException e) {
throw e;
}
}
}