栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Android App中读取CSV文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Android App中读取CSV文件

尝试OpenCSV-这将使您的生活更轻松。

首先,将此包添加到您的

gradle
依赖项中,如下所示

implementation 'com.opencsv:opencsv:4.6'

那你要么做

import com.opencsv.CSVReader;import java.io.IOException;import java.io.FileReader;...try {    CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));    String[] nextLine;    while ((nextLine = reader.readNext()) != null) {        // nextLine[] is an array of values from the line        System.out.println(nextLine[0] + nextLine[1] + "etc...");    }} catch (IOException e) {}

要么

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));List myEntries = reader.readAll();

评论后编辑

try {    File csvfile = new File(Environment.getExternalStorageDirectory() + "/csvfile.csv");    CSVReader reader = new CSVReader(new FileReader(csvfile.getAbsolutePath()));    String[] nextLine;    while ((nextLine = reader.readNext()) != null) {        // nextLine[] is an array of values from the line        System.out.println(nextLine[0] + nextLine[1] + "etc...");    }} catch (Exception e) {    e.printStackTrace();    Toast.makeText(this, "The specified file was not found", Toast.LENGTH_SHORT).show();}

如果您想将

.csv
文件与应用程序打包并在安装应用程序时将其安装在内部存储中,请
assets
在项目
src/main
文件夹(例如
c:myappappsrcmainassets
)中创建一个文件夹,然后将
.csv
文件放在其中,然后在您的活动中像这样引用它:

String csvfileString = this.getApplicationInfo().dataDir + File.separatorChar + "csvfile.csv"File csvfile = new File(csvfileString);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/453199.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号