如何在android应用程序中解析CSV文件? [英] How to parse the CSV file in android application?

查看:63
本文介绍了如何在android应用程序中解析CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 drawable/asset 文件夹中有一个 CSV 文件.在 CSV 文件中有四列.第一个用于日期,其余三个用于整数数据.

I have a CSV file in drawable/asset folder. In the CSV file there are four columns. First one is for date and rest three are for integer data.

我需要解析这个 CSV 文件并将数据保存在单独的数组中.

I need to parse this CSV file and save the data in separate arrays.

我已经寻找了解决方案,但我不知道如何做到这一点.

I have searched for a solution, but I don't get proper idea on how to do this.

推荐答案

我喜欢这个 csv 阅读器:https://mvnrepository.com/artifact/net.sf.opencsv/opencsv/2.3

I like this csv reader: https://mvnrepository.com/artifact/net.sf.opencsv/opencsv/2.3

只需将其添加到您的项目中即可.

Just add it to your project.

示例代码(假设有文件 assets/test.csv):

Example code (assuming there is the file assets/test.csv):

        String next[] = {};
        List<String[]> list = new ArrayList<String[]>();

        try {
            CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("test.csv")));
            while(true) {
                next = reader.readNext();
                if(next != null) {
                    list.add(next);
                } else {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

您可以访问导入的数据,例如

You can access the imported data with, for example,

list.get(1)[1]

那将返回一个字符串.

这篇关于如何在android应用程序中解析CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆