简体   繁体   中英

How can I read a CSV file in android

The timetable.csv file is in the same directory as the 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)

You need to create assets Folder in app module. Paste your .csv file there. Then you can read a file like this

try
{

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

......
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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