我有我的Activity類中加載和保存文件的代碼。它工作正常。代碼保存了cFavretClass的內容。我正在清理代碼,所以我將文件I/O移到了cFavret類中。你需要在Activity類中有文件I/O嗎?
我無法獲得編譯的代碼。現在我收到一個錯誤,說openFileOutput is undefined in type cFavrets
。
我假設這個方法是在谷歌活動類中聲明的? 這是否意味着所有的文件I/O都必須在活動類中?
boolean Save()
{
String FILENAME = "hello_file";
try {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(buffer);
fos.close();
}
// just catch all exceptions and return false
catch (Throwable t) {
return false;
}
return true;
}
boolean Load()
{
String FILENAME = "hello_file";
try {
FileInputStream fos = openFileInput(FILENAME);
buffer[0]=0;
fos.read(buffer);
fos.close();
}
// just catch all exceptions and return false
catch (Throwable t) {
// maybe file does not exist, try creating it
return false;
}
return true;
}
嗨,在Activty類中,我怎麼能得到上下文???我正在嘗試Context mContext; mContext = Context;不工作 –
活動是上下文 - 只需傳遞「this」即可 – JRaymond