0

I've created a simple Android app that displays text to the user.

Now I'm attempting to implement a CSVReader to retrieve text from a CSV file. After hours of trying different things.

I finally implemented an open source CSVReader (At least it's not giving me any compile errors anymore).

Now when I run the app, it crashes and I get a "file not found" exception. Either I'm not placing my CSV file in the correct location, I'm not referencing the correct file path, or both.

I've tried using the absolute file path (ex. starting with my C:/Users/Tim/AndroidStudioProjects/...).

I've also tried using a relative path starting with the Assets folder (ex. "Assets/SOCIAL_POSTS.csv") and nothing I've tried has worked.

I've tried looking for similar Questions on Stack Overflow, and I've tried several variations of file paths and nothing has worked so far. What do you think I should try next?

Here is a link to this project on GitHub.

The code pointing to the CSV file is under

app > src > main > java > com > example > tim > inspiredquestions

The CSV file is called SOCIAL_POSTS.csv and it is under

Assets > SOCIAL_POSTS.csv

Final note: I've used StackOverflow for debugging help for a year now, but this is the first question I've asked.

Thank-you for your patience! I'm trying to be as self-reliant as I can, but I'm going on a limb and asking for help. I'm sure this problem has a simple answer I'm overlooking.

Mackan
  • 6,200
  • 2
  • 25
  • 45
Timothy Steele
  • 773
  • 1
  • 7
  • 19
  • You need to create your assets folder in app/src/main/assets – WISHY Apr 23 '15 at 05:55
  • which java file is reading csv file? – Naman Gala Apr 23 '15 at 05:59
  • Game.java is reading the csv file. Also I forgot to push up my most recent code. Please pull from master to get the updated code. Sorry for the lack of clarity / hassle. – Timothy Steele Apr 23 '15 at 06:10
  • Can you change `/` with \\ in your path, and try your code once again. – Naman Gala Apr 23 '15 at 06:20
  • I changed '/' with // in my path (you did mean thoughout the entire file path right?) I'm getting the same error. – Timothy Steele Apr 23 '15 at 06:29
  • I finally figured out the answer. I added final 'CSVReader csvReader = new CSVReader(new InputStreamReader( c.getAssets().open("SOCIAL_POSTS.csv")' after looking at the answer to this question: [link] (stackoverflow.com/questions/28979778). (@WISHY I had an assets folder under app/src/main before but I didn't push up the code until recently. But until now I didn't know how to access the file properly). So then the getAssets() function access my file inside of 'app > src > main > assets' ? – Timothy Steele Apr 23 '15 at 07:07

2 Answers2

0

I finally figured out the answer. I added final CSVReader csvReader = new CSVReader(new InputStreamReader(c.getAssets().open("SOCIAL_POSTS.csv") to my load function in Game.java after looking at the answer to this question. Previously I had put my CSV file in an assets folder under app/src/main but I didn't know how to access the file properly until reviewing that SO post.

Community
  • 1
  • 1
Timothy Steele
  • 773
  • 1
  • 7
  • 19
0

How I use Android Studio for files is to position my files in the raw folder, then push them to the /data/data/yourapplication/files directory. I do this through Tools/Android/Android Device Monitor and navigate to the /data/data/yourapplication/files directory and select an icon in the upper right that says "Push a file onto the device", then navigate to the raw files in my app and select the csv file(s) I want placed in the /files directory. From that point I aim my stream readers at that directory, in my case, /data/data/app/com.android/example/darrell/MenuPlanner/files Directory. Files addressed here use the file extension and do not need the path specified:

    public List<EntreeData>     ReadEntreesFromFilesDir(Context inContext) {
    this.mContext = inContext;
    List<String> lines = new ArrayList<String>();
    String[] separated;
        try {
            FileInputStream fis = mContext.openFileInput("entrees.csv");
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader bufferedReader = new BufferedReader(isr);
            while ((mLine = bufferedReader.readLine()) != null) {
                if (mLine == null) break;
                lines.add(mLine);
            }
            fis.close();
            isr.close();
        } catch (FileNotFoundException e) {
            Log.d("FILE ERROR", e.getMessage() + " not found");
        } catch (IOException ioe) {
            Log.d("FILE ERROR", ioe.getMessage());
        }
    for (int x = 0;x<lines.size();x++){
        separated = lines.get(x).split(",");
        EntreeData thisEntree = new EntreeData();
        thisEntree.setEntreeName(separated[0].trim());
        thisEntree.setCategory(separated[1].trim());
        thisEntree.setSubcategory(separated[2].trim());
        thisEntree.setRecipe(separated[3].trim());
        mEntreeList.add(thisEntree);
    }
    return mEntreeList;
}

As a fallback I read from /res/raw directory which is not writeable and I am assured of file integrety. Files here do not use an extension, so point your stream reader at a file without using the extension name. Also use the asset manager to access these files:

public List<EntreeData>     ReadEntreesFileFromRaw(Context inContext) {
    this.mContext = inContext;
    List<String> lines = new ArrayList<String>();
    String[] separated;    
    AssetManager assetManager = mContext.getAssets();
    try {
        InputStream inputStream = mContext.getResources().openRawResource(R.raw.entrees);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        int lineCount = 0;
        while ((mLine = reader.readLine()) != null) {
            separated = mLine.split(",");
            EntreeData entreeEntry = new EntreeData();
            if (mLine == null) break;
            separated = mLine.split(",");
            entreeEntry.setEntreeName(separated[0].trim());
            entreeEntry.setCategory(separated[1].trim());
            entreeEntry.setSubcategory(separated[2].trim());
            entreeEntry.setRecipe(separated[3].trim());
            mEntreeList.add(lineCount, entreeEntry);
            lineCount++;
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.d("FILE ERROR", e.getMessage() + " not found");
    } catch (IOException ioe) {
        Log.d("FILE ERROR", ioe.getMessage());
    }

    return mEntreeList;
}

Good luck and happy programming!