android读写csv文件内容,【 Android 】CSV 文件在 Android 开发中的应用

Android 从服务器读取数据通常情况下都是读取的 Json 数据,其实 .csv 文件也有同样的效果。本文将围绕两方面进行讲解:① 读取 .csv 文件 ② 将数据写入到 .csv 文件里。

一、读取 .csv 文件

① .csv文件是可以用 Excel 或者记事本打开和编辑的。先看一个示例文件

9419f0cc150cdc318af0efbdfd7eafc7.png

.csv 文件

② 读取.csv文件,并显示在 UI 上

通过示例图,我们需要创建一个bean拿到id、name、age的getter和setter方法。

public class TestDataBean {

private int id;

private String name;

private int age;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public TestDataBean(int id, String name, int age) {

this.id = id;

this.name = name;

this.age = age;

}

}

③ 公开一个读取.csv文件的方法

private ArrayList readCsv(String path) {

ArrayList readerArr = new ArrayList<>();

File file = new File(path);

FileInputStream fileInputStream;

Scanner in;

try {

fileInputStream = new FileInputStream(file);

in = new Scanner(fileInputStream, "UTF-8");

in.nextLine();

while (in.hasNextLine()) {

String[] lines = in.nextLine().split(",");

TestDataBean bean = new TestDataBean(Integer.parseInt(lines[0]), lines[1], Integer.parseInt(lines[2]));

readerArr.add(bean);

}

} catch (Exception e) {

e.printStackTrace();

}

return readerArr;

}

④ 通过adb命令将.csv文件写入到包名下的file文件夹里面<

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值