我正在使用以下 try/catch 尝试将管道分隔的文本文件解析为抽认卡应用程序的数组(每行如下: spanishword|englishword|spanishword.mp3 )。很简单,但我是个十足的菜鸟。这是我拼凑的内容,它导致了 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);
}
最佳答案
The file is res/raw/first100mostcommon.txt
那不是一个文件。那是一种原始资源。它作为文件存在于您的开发计算机上。它存在于设备上 APK(ZIP 存档)的条目中。
要访问存储在开发计算机上的原始资源 res/raw/first100mostcommon.txt
,请调用 getResources().openRawResource(R.raw.first100mostcommon)
任何上下文
,例如您的 Activity
。这将返回一个 InputStream
,您可以用它来读取内容。
关于java - FileNotFoundException:ENOENT...但是,但是,但是我有一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17798743/