我正在使用以下 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);
}