FileNotFoundException:ENOENT ...但是,但是,我有一个文件

时间:2013-07-22 22:47:42

标签: java android filenotfoundexception

我正在使用以下try / catch尝试将管道分隔的文本文件解析为数组(每行如下:spanishword | englishword | spanishword.mp3),用于flashcard应用程序。很简单,但我是一个完整的菜鸟。这是我拼凑在一起的内容,这会导致FileNotFoundException

该文件是res / raw / first100mostcommon.txt。我喜欢解决问题,并且我对提交解决方案并不感兴趣,但是比“未找到文件”更好的提示将不胜感激。

我认为String strFile = "R.raw.first100mostcommon";是命名它的正确方法;这是对的吗?

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);

}  

1 个答案:

答案 0 :(得分:4)

  

该文件是res / raw / first100mostcommon.txt

那不是文件。这是一种原始资源。它作为开发计算机上的文件存在。它存在于设备上的APK(ZIP存档)中的条目中。

要以[{1}}方式访问存储在开发计算机上的原始资源,请在res/raw/first100mostcommon.txt上调用getResources().openRawResource(R.raw.first100mostcommon),例如Context。这将返回Activity,您可以使用它来阅读内容。