【问题标题】:Read streaming data from csv using OpenCSV使用 OpenCSV 从 csv 读取流数据
【发布时间】:2017-02-02 01:23:25
【问题描述】:

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

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

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

【问题讨论】:

  • 您使用的是什么版本的 openCSV?在 3.0-3.4 版本中从 Streams 读取存在一个已知问题。请更新到 3.8 版本并试一试。
  • @PeterSmith 我正在使用 3.8 版本的 Opencsv。实际上我正在尝试实时读取流数据,为此我将不得不使用线程。

标签: java android opencsv


【解决方案1】:

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

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

因此,如果这些示例中的任何一个显示多行,请检查您的文件是否仅包含一行,也许您正在将文件中的所有数据全部写入第一行或类似的内容。

【讨论】:

  • 我尝试了两种方式,但仍然没有打印第一行。有数千行,因为数据以每秒 50 行的速度传输。
  • 现在我可以读取数据了,实际上早先我把上面的代码放在文件执行之前。仍然读取部分数据,希望在流式传输发生时实时读取数据。
猜你喜欢
  • 1970-01-01
  • 2022-11-12
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
相关资源
最近更新 更多