I try to readout a csv-File. In my Code I readout the Lines in the csv-File. Therefor I use an arraylist to add each item in the line to the arraylist.
After reading out a line i storte the array list in an other array list and then go to read out the next line and so on and ao on.
Now I have here a few Questions and one Problem.
1) When I add an item to an arraylist is it always added at the en of the list (When I give no specific place where to add the item like in my code)? With this then the order of the csv-file would be keeped.
2) I figuered out, that when I clear the arraylist datatemp to prepare the reading of the next line, also the content of the array list data is cleared. How can I prevent my code from doing so?
3) Is there a way to make an normal array out of the arraylist data I have?
4) Is my way of reading out the csv-File correct or is there any better way?
public class CSVReader {
public void ReadCSV (String csvfilepath) {
BufferedReader CSVFile = null;
String dataRow = null;
ArrayList<Float> datatemp = new ArrayList<Float>();
ArrayList<ArrayList<Float>> data = new ArrayList<ArrayList<Float>>();
try {
CSVFile = new BufferedReader(new FileReader(csvfilepath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
dataRow = CSVFile.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (dataRow != null){
String[] dataArray = dataRow.split(",");
for (String item:dataArray) {
datatemp.add(Float.valueOf(item));
}
try {
dataRow = CSVFile.readLine();
} catch (IOException e) {
e.printStackTrace();
}
data.add(datatemp);
datatemp.clear();
}
try {
CSVFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}