简体   繁体   English

如何在android中读取CSV文件

[英]How can I read a CSV file in android

The timetable.csv file is in the same directory as the MainActivity.java. timetable.csv文件与MainActivity.java位于同一目录中。 I used the following function. 我使用了以下功能。

public void getaction(String date, TextView action1) {

    String line = "";

    String[] football = new String[20];
    try {
        System.out.print("Hello");
        BufferedReader br = new BufferedReader(new FileReader("timetable.csv"));
        String headerLine = br.readLine();
        System.out.print("Hello");
        while ((line = br.readLine()) != null) {
            football = line.split(",");
            //match the date
            if(football[0].equals(date)){
                action1.setText("i found chelsea");
            }else{
                action1.setText("I did not find Chelsea!");
            }
        }


    }
    catch(Exception io){
            System.out.println(io);

        }

    action1.setText("I did too find Chelsea!");
}

It gave the following error: 它给出了以下错误:

I/System.out: Hellojava.io.FileNotFoundException: timetable.csv (No such file or directory) I / System.out:Hellojava.io.FileNotFoundException:timetable.csv(没有这样的文件或目录)

You need to create assets Folder in app module. 您需要在app模块中创建资源文件夹。 Paste your .csv file there. 将.csv文件粘贴到那里。 Then you can read a file like this 然后你可以读取这样的文件

try
{

reader = new BufferedReader(
    new InputStreamReader(getAssets().open("filename.csv")));
......
}
catch (Exception e)
{

......
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM