0

I'm using the following try/catch to try to parse a pipe-separated text file into an array ( each line is like this: spanishword|englishword|spanishword.mp3 ) for a flashcard app. Pretty simple, but I'm a complete noob. Here is what I cobbled together, which leads to the FileNotFoundException.

The file is res/raw/first100mostcommon.txt. I like problem solving and am not really interested in being handed a solution, but a better hint than "file not found" would be appreciated.

I think String strFile = "R.raw.first100mostcommon"; is the right way to name it; is this right?

try 
{
    String strFile = "R.raw.first100mostcommon";

    //create BufferedReader to read pipe-separated variable file

    BufferedReader br = new BufferedReader( new FileReader(strFile));
    String strLine = "";
    StringTokenizer st = null;

    int row = 0; 
    int col = 0;

    //read pipe-separated variable file line by line

    while( (strLine = br.readLine()) != null)
    {
        //break pipe-separated variable line using "|"
        st = new StringTokenizer(strLine, "|");

        while(st.hasMoreTokens())
        {
            //store pipe-separated variable values
            stWords[row][col] = st.nextToken();
            col++;
        }
        row++;                                   
        //reset token number
        col = 0;                          
    }     
}
catch(Exception e)
{
    text.setText("Exception while reading csv file: " + e);

}  
Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
Peltier Cooler
  • 157
  • 1
  • 12
  • logcat would be nice ;-) other than that, see my solution comingsoon® - edit: no solution: see http://stackoverflow.com/a/5675934/2188682 – Christian R. Jul 22 '13 at 22:55
  • 1
    You should be using proper file seperator. like `/` instead of `.` in `strFile`. Currently compiler looking this as complete file name and not as `filepath`. So most probably its looking in `root` folder where that file is not available causing `FileNotFoundException` – Smit Jul 22 '13 at 22:56
  • Is your file in the same directory as your `.class` file? – PM 77-1 Jul 22 '13 at 23:00

1 Answers1

4

The file is res/raw/first100mostcommon.txt

That is not a file. That is a raw resource. It exists as a file on your development machine. It exists in an entry in the APK (ZIP archive) on the device.

To access the raw resource stored on your development machine as res/raw/first100mostcommon.txt, call getResources().openRawResource(R.raw.first100mostcommon) on any Context, such as your Activity. This will return an InputStream that you can use to read in the contents.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491