Question

I'm having trouble reading from a csv file for my Android project. I'm using a Mac, with Eclipse ADT, and have imported OpenCSV.

The problem that I keep running into is that the file is not found. I have tried putting it everywhere, including: in the root folder (where the src folder is located), inside the src folder, and inside res/raw. I have refreshed the view, I have cleaned and rebuilt the project, I have restarted Eclipse, I have tried importing the file by drag and drop as well as by using the import option. For some reason, it still refuses to be found.

When I look at its path and absolute path (using file.getPath() and file.getAbsolutePath() they are: "abc.csv" and "/abc.csv" respectively. I have also double checked the file name, it is not named abc.csv.csv or anything similar.

I've used the following lines of code and these are the ones that have returned the error:

  1. (This is from the OpenCSV site.)

    CSVReader reader = new CSVReader(new FileReader("abc.csv"));
    
  2. (#2 and #3 are not from OpenCSV and are just me experimenting around.)

    AssetFileDescriptor descriptor = getAssets().openFd("abc.csv");
    CSVReader reader = new CSVReader(new FileReader(descriptor.getFileDescriptor()));
    

3.

    File file = new File("abc.csv");
    try
    {
        Scanner inputStream = new Scanner(file);
        while(inputStream.hasNext())
        {
            String data = inputStream.next();
        }
        inputStream.close();
    } 
    catch(FileNotFoundException e)
    {
        Log.i(TAG, "File not found.");
    }
Was it helpful?

Solution

Place the file in the assets folder and use AssetManager.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top