我正在尝试将文本写入文件,但我似乎无法让它工作。
public static void saveState(){
String data = age + "," ;
FileOutputStream fos;
Context con = getApplicationContext();
try {
fos = con.openFileOutput("state", 0);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos);
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
我环顾四周,似乎无法在没有上下文的情况下调用 openFileOutput(),但不知道“Context con = getApplicationContext()”在 Activity 之外无法正常工作。它只是告诉我该类型未定义 getapplicationcontext 。有谁可以帮我离开这里吗?