1

当我尝试保存我的 android 游戏(通过我的 Windows 笔记本电脑上的 Android 模拟器玩)时,我得到一个FileNotFoundException. 我花了几个小时尝试不同的东西,但仍然感到困惑,因为代码在我以前的版本上完美运行,而不是在 Android 上运行。

FileOutputStream saveStream;
ObjectOutputStream savePlayerObject = null;
String destinationFile = player1.getName() + ".txt";

try
{
   saveStream = new FileOutputStream(destinationFile);    
   savePlayerObject = new ObjectOutputStream(saveStream);   
   savePlayerObject.writeObject(player1);
}

catch(FileNotFoundException ex)
{
   Toast toast = Toast.makeText(getApplicationContext(), "Save Failed", Toast.LENGTH_LONG);
   toast.show();
}


catch(IOException ex)
{
   Toast toast = Toast.makeText(getApplicationContext(), "Save Failed", Toast.LENGTH_LONG);
   toast.show();
}


finally
{
   try
   {
      if(savePlayerObject !=null)
      {
         savePlayerObject.flush();    
         savePlayerObject.close();   
         Toast toast = Toast.makeText(getApplicationContext(), "Thank-You For Playing, See You Soon", Toast.LENGTH_LONG);
         toast.show();   
         System.exit(0);   
      }
   }

   catch(IOException ex)
   {
      Toast toast = Toast.makeText(getApplicationContext(), "Save Failed", Toast.LENGTH_LONG);
      toast.show();
   }
}
4

2 回答 2

1

这意味着程序无法在指定位置创建文件。

String destinationFile = player1.getName() + ".txt";

该文件将在当前工作目录中创建。这通常不是你想要的。您需要考虑文件的确切位置,然后指定绝对路径。

于 2013-01-16T22:43:15.097 回答
0

也许您没有创建新文件的必要权限。

于 2013-01-16T22:43:32.260 回答