Read streaming data from csv using OpenCSV

空扰寡人 提交于 2019-12-23 03:42:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!