I have a java function that reads a csv and returns its content as a Vector<Vector<String>>
.
The function seems to work as I need it, but my gut feeling says it could be better (never mind the fact that it is declared throws Exception
).
So here it is:
private static Vector<Vector<String>> readTXTFile(String csvFileName) throws Exception {
BufferedReader stream = new BufferedReader(
new InputStreamReader(
new FileInputStream(csvFileName)));
Vector<Vector<String>> csvData = new Vector<Vector<String>>();
String line;
while ((line = stream.readLine()) != null) {
csvData.add(new Vector<String>() );
String[] values = line.split(",");
for (int v=0; v<values.length; v++) {
csvData.get(csvData.size()-1).add(values[v]);
}
}
return csvData;
}
Background
Ultimately, the CSV Data will be used to fill a JTable. I could have used an String[][]
for the data too, but it seemed that constructing a dynamic String[][]
from a csv file would have been even more combersome (although I stand ready to be corrected on this, too).