我正在 netbeans 中开发一个 android 应用程序。我正在尝试使用 opencsv 读取 CSV 文件。当我将文件放入资源文件夹并尝试从那里读取它时,在构建无效资源目录时出现错误。我应该在哪里存储 csv 文件,以便每次应用程序启动时都可以读取它?
问问题
23041 次
5 回答
15
您应该将 csv 文件放在资产文件夹中..
InputStreamReader is = new InputStreamReader(getAssets()
.open("filename.csv"));
BufferedReader reader = new BufferedReader(is);
reader.readLine();
String line;
while ((line = reader.readLine()) != null) {
}
于 2013-11-14T10:13:53.640 回答
11
一些建议;
- 创建一个对象,用于将一行数据保存到 csv 中。(
Ex: YourSimpleObject
. 它使您可以轻松管理数据。) - 逐行读取文件并分配给对象。将对象添加到列表中。(例如
ArrayList<YourSimpleObject >
:)
代码:
private void readAndInsert() throws UnsupportedEncodingException {
ArrayList<YourSimpleObject > objList= new ArrayList<YourSimpleObject >();
AssetManager assetManager = getAssets();
InputStream is = null;
try {
is = assetManager.open("questions/question_bank.csv");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String line = "";
StringTokenizer st = null;
try {
while ((line = reader.readLine()) != null) {
st = new StringTokenizer(line, ",");
YourSimpleObject obj= new YourSimpleObject ();
//your attributes
obj.setX(st.nextToken());
obj.setY(st.nextToken());
obj.setZ(st.nextToken());
obj.setW(st.nextToken());
objList.add(sQuestion);
}
} catch (IOException e) {
e.printStackTrace();
}
}
于 2013-11-14T11:09:05.650 回答
4
作为替代方案,请查看uniVocityParsers。它提供了大量解析分隔文件的方法。下面的示例将一个 Csv 文件(见下图)从 res/raw 文件夹加载到 InputStream 对象中,并以列的方式读取它(key=Column & value=ColumnValues 的映射)。
//Gets your csv file from res/raw dir and load into a InputStream.
InputStream csvInputStream = getResources().openRawResource(R.raw.calendario_bolsa);
//Instantiate a new ColumnProcessor
ColumnProcessor columnProcessor = new ColumnProcessor();
//Define a class that hold the file configuration
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.getFormat().setLineSeparator("\n");
parserSettings.setHeaderExtractionEnabled(true);
parserSettings.setProcessor(columnProcessor);
//Creates a new CsvParser, passing the settings into its construtor:
CsvParser csvParser = new CsvParser(parserSettings);
//Calls parse method, instantiating an InputStreamReader, passing to its constructor the InputStream object
csvParser.parse(new InputStreamReader(csvInputStream));
//Gets the csv data as a Map of Column / column values.
Map<String, List<String>> columnarCsv = columnProcessor.getColumnValuesAsMapOfNames();
要将 univocityParsers 添加到您的 Android 项目中:
compile group: 'com.univocity', name: 'univocity-parsers', version: '2.3.0'
于 2016-12-24T05:02:51.047 回答
2
使用 opencsv:
InputStream is = context.getAssets().open(path);
InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
List<String[]> csv = new CSVReader(reader).readAll();
于 2016-11-02T20:05:30.310 回答
1
您可以使用此代码
try {
InputStream csvStream = assetManager.open(CSV_PATH);
InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
CSVReader csvReader = new CSVReader(csvStreamReader);
String[] line;
// throw away the header
csvReader.readNext();
while ((line = csvReader.readNext()) != null) {
questionList.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
您可以从http://sourceforge.net/projects/opencsv/files/latest/download下载 csvreader 文件
并导入您的项目
于 2013-11-14T10:19:18.293 回答