繁体   English   中英

使用 OpenCSV 从 csv 读取流数据

[英]Read streaming data from csv using OpenCSV

我有保存在下载文件夹中的加速度计和陀螺仪传感器流数据。 我想实时读取所有数据或逐行读取数据流,但我无法超越第一行。

 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();
     }

在输出中只打印第一行。 我需要阅读每一行,以便我可以做进一步的操作。

Int 文档显示了两种不同的方法:

1-迭代器样式模式:

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- 使用列表:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv")); 
List<String[]> myEntries = reader.readAll(); 

for(String[] item : myEntries)
    System.out.println(item);

因此,如果这些示例中的任何一个向您显示不止一行,请检查您的文件是否只包含一行,也许您将所有数据都写入文件的第一行或类似内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM