-1
我正嘗試將res/raw /中的csv文件讀入SQLite數據庫。這是我的功能:如何在try/catch塊之外引用一個BufferReader變量
public void updateDatabase(Context context, SQLiteDatabase database) {
InputStream inputStream = context.getResources().openRawResource(R.raw.teamlist);
try {
BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
} catch (UnsupportedEncodingException ioe) {
Log.e("ERROR", "Could not load " + ioe);
}
String line = "";
database.beginTransaction();
try {
while ((line = buffer.readLine()) != null) {
// read each line from CSV file into a database
}
} catch (IOException ioe){
Log.e("ERROR", "Could not load " + ioe);
}
database.setTransactionSuccessful();
database.endTransaction();
}
但我在while循環中得到錯誤「無法解析符號'緩衝區'」。如何在try函數外引用BufferReader?我嘗試使用「null」初始化try塊外部的緩衝區讀取器,但這導致我的應用程序崩潰。有什麼建議麼?
有道理。我會改變它,並給它一個鏡頭。感謝您的建議! – NBC
它的工作,感謝您的幫助! – NBC