繁体   English   中英

如何在android中读取CSV文件

[英]How can I read a CSV file in 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(没有这样的文件或目录)

您需要在app模块中创建资源文件夹。 将.csv文件粘贴到那里。 然后你可以读取这样的文件

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