如何在Android中读取CSV文件

时间:2019-03-20 06:25:35

标签: java android

timetable.csv文件与MainActivity.java位于同一目录中。我使用了以下功能。

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!");
}

它给出了以下错误:

  

I / System.out:Hellojava.io.FileNotFoundException:timetable.csv(否   这样的文件或目录)

1 个答案:

答案 0 :(得分:0)

您需要在应用模块中创建资产文件夹。将您的.csv文件粘贴到此处。 然后您可以读取这样的文件

try
{

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

......
}