58 lines
1.4 KiB
Java
58 lines
1.4 KiB
Java
package a6;
|
||
|
||
import java.io.File;
|
||
import java.io.FileReader;
|
||
import java.io.BufferedReader;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
|
||
public class CsvReader {
|
||
|
||
private HashMap<String, Integer> columns = new HashMap<String, Integer>();
|
||
private ArrayList<String> values = new ArrayList<String>();
|
||
private ArrayList<String> strings = new ArrayList<String>();
|
||
private int curr_line = 0;
|
||
|
||
public boolean load(String filename) {
|
||
boolean ret = SysUtils.readStrings(filename, strings);
|
||
if (ret) {
|
||
if (nextLine()) {
|
||
for (int i = 0; i < values.size(); ++i) {
|
||
columns.put(values.get(i), i);
|
||
}
|
||
}
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
public boolean nextLine() {
|
||
if (curr_line >= strings.size()) {
|
||
return false;
|
||
}
|
||
values.clear();
|
||
for (String val : strings.get(curr_line).split(",")) {
|
||
values.add(val);
|
||
}
|
||
++curr_line;
|
||
return true;
|
||
}
|
||
|
||
public XValue getValue(String key) {
|
||
if (!keyExists(key)) {
|
||
return new XValue();
|
||
}
|
||
String val = values.get(columns.get(key));
|
||
return new XValue(val
|
||
.replaceAll("\\n", "\n")
|
||
.replaceAll(",", ",")
|
||
);
|
||
}
|
||
|
||
public boolean keyExists(String key) {
|
||
return columns.containsKey(key);
|
||
}
|
||
|
||
}
|