我在一个活动的内部存储器中创建了一个文件,并希望在下一个活动中再次在其上写入文件,但遇到此错误。
在第一个活动中:

String id_file = "tt_id";
String key_file = "tt_key";
FileOutputStream outputStream1;
FileOutputStream outputStream2;
try {
   outputStream1 = openFileOutput(id_file, Context.MODE_PRIVATE);
   outputStream2 = openFileOutput(key_file, Context.MODE_PRIVATE);
   outputStream1.write(id.getBytes());
   outputStream2.write(key.getBytes());
   outputStream1.close();
   outputStream2.close();


但在第二个活动中:

FileOutputStream outputStream1;
FileOutputStream outputStream2;
String id="dfg";
String key="khdfks";
    outputStream1 = openFileOutput("tt_id",Context.MODE_PRIVATE);
    outputStream2 = openFileOutput("tt_key", Context.MODE_PRIVATE);
outputStream1.write(id.getBytes());
outputStream2.write(key.getBytes());
outputStream1.close();
outputStream2.close();


我刚刚开始进行android应用开发,因此感谢您的帮助。先感谢您

最佳答案

我认为您需要在读取/写入之前指定创建的文件位置。还要确保文件存在。否则它将引发文件未找到异常。为此,请尝试此操作

File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.txt");
file.createNewFile();
if(file.exists())
{
    OutputStream outputStream1 = new FileOutputStream(file);
    String id="dfg";
    outputStream1.write(id.getBytes());
    outputStream1.close();
}

10-08 11:01