[英]FileNotFoundException open failed: ENOENT (No such file or directory)
[英]FileNotFoundException: ENOENT…but, but, but I have a file
我正在使用以下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);
}
該文件是res / raw / first100mostcommon.txt
那不是文件。 那是原始資源。 它作為文件存在於您的開發計算機上。 它存在於設備上APK(ZIP存檔)中的一個條目中。
要訪問以res/raw/first100mostcommon.txt
形式存儲在開發計算機上的原始資源,請在任何Context
(例如Activity
getResources().openRawResource(R.raw.first100mostcommon)
上調用getResources().openRawResource(R.raw.first100mostcommon)
。 這將返回一個InputStream
,您可以使用該InputStream
讀取內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.