尝试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);



