我有保存在下载文件夹中的加速度计和陀螺仪传感器流数据。 我想实时读取所有数据或逐行读取数据流,但我无法超越第一行。
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);
因此,如果这些示例中的任何一个向您显示多行,请检查您的文件是否只包含一行,也许您正在将文件中的所有数据都写在第一行或类似的地方。
关于java - 使用 OpenCSV 从 csv 读取流数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39673372/