问题
I have Accelerometer and Gyro sensor streaming data which is saved in Download folder. I want to read all the data or line by line as the data stream in real time,but i am not able to go beyond first line.
try {
CSVReader reader = newCSVReader(newFileReader(path.getAbsoluteFile()));
{
List<String[]>allRows = reader.readAll();
for (String[]row :allRows)
Log.i(TAG1,Arrays.toString(row));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
in the output only first line is printed. I need to read each line so that i can do further operation.
回答1:
Int the documentation shows two different ways to do it:
1- Iterator style pattern:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null)
{
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
2- With a List:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List<String[]> myEntries = reader.readAll();
for(String[] item : myEntries)
System.out.println(item);
So, if any of these example shows you more than one line, check if your file contains just one line, maybe you are writing all your data in the file all in the first line or something like that.
来源:https://stackoverflow.com/questions/39673372/read-streaming-data-from-csv-using-opencsv