读取资源文件夹android中的CSV文件

时间:2013-11-14 10:03:38

标签: android opencsv

我正在开发netbeans的Android应用程序。我正在尝试使用opencsv读取CSV文件。当我将文件放在资源文件夹中并尝试从那里读取它时,在构建说无效资源目录时出错。我应该在哪里存储csv文件,以便每次应用程序启动时都可以读取它?

5 个答案:

答案 0 :(得分:10)

你应该将csv文件放在assets文件夹中..

InputStreamReader is = new InputStreamReader(getAssets()
                        .open("filename.csv"));

                BufferedReader reader = new BufferedReader(is);
                reader.readLine();
                String line;
                while ((line = reader.readLine()) != null) {

                    }

答案 1 :(得分:8)

一些建议;

  • 创建一个对象,用于将一行数据保存到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();
            }



}

答案 2 :(得分:2)

作为替代方案,请查看uniVocityParsers。它提供了大量解析分隔文件的方法。下面的示例将res / raw文件夹中的Csv文件(见下图)加载到InputStream对象中,并以colunar方式读取它(key = Column&amp; value = ColumnValues的地图)。

calendario_bolsa.csv

//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'

答案 3 :(得分: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();
              }

您可以从中下载csvreader文件 http://sourceforge.net/projects/opencsv/files/latest/download

并导入项目

答案 4 :(得分:0)

使用opencsv:

InputStream is = context.getAssets().open(path);
InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
List<String[]> csv = new CSVReader(reader).readAll();