我想读取CSV文件。问题是当我打印0或任何其他索引值时,显示的尴尬结果如您在快照中看到的那样。附带快照。
从以下代码读取值:
InputStream inputStream = getResources().openRawResource(R.raw.stats);
FileReader csvFile = new FileReader(inputStream);
List<String[]> scoreList = csvFile.read();
for(String[] scoreData:scoreList ) {
Toast.makeText(this, scoreData[1], Toast.LENGTH_SHORT).show();
}
FileReader类:
package com.example.zohaib.ultimatesmsblocker;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FileReader {
InputStream inputStream;
public FileReader(InputStream inputStream){
this.inputStream = inputStream;
}
public List<String[]> read(){
List<String[]> resultList = new ArrayList();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null) {
String[] row = csvLine.split(",");
resultList.add(row);
}
}
catch (IOException ex) {
throw new RuntimeException("Error in reading CSV file: "+ex);
}
finally {
try {
inputStream.close();
}
catch (IOException e) {
throw new RuntimeException("Error while closing input stream: "+e);
}
}
return resultList;
}
}
您尝试加载的文件不是CSV
,而是XLSX
。
并非每个Excel文件都是一个CSV
文件。您需要将其导出到CSV
使用Excel。
Excel的.xlsx
文件实际上是XML
打包成的某些格式的文件ZIP
。在PK
你看到的前两个字母代表了ZIP
头。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句