1

所以,这是我拥有的代码:

try  
{  
  PlayerSave save = new PlayerSave(this);   
  save.playerLooks = look;        
  File test = new File("C:/cache/" + playerName + ".tmp");  
  test.createNewFile();  
  FileOutputStream f_out = new  
      FileOutputStream("C:/cache/" + playerName + ".tmp");  
  ObjectOutputStream obj_out = new ObjectOutputStream (f_out);  
  obj_out.writeObject (save);  
  obj_out.close();  
  f_out.close();  
}  
catch (Exception e)  
{  
  e.printStackTrace();  
} 

执行后,我收到以下错误:

java.io.FileNotFoundException: C:\cache\Bobdole.tmp (The system cannot find the path specified)

我也尝试过使用这段代码:

    try
    {
      PlayerSave save = new PlayerSave(this); 
      save.playerLooks = look;      
//      File test = new File("C:/cache/" + playerName + ".tmp");
//      test.createNewFile();
      FileOutputStream f_out = new
          FileOutputStream("C:/cache/" + playerName + ".tmp");
      ObjectOutputStream obj_out = new ObjectOutputStream (f_out);
      obj_out.writeObject (save);
      obj_out.close();
      f_out.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

但是,它会产生相同的错误。我很困惑为什么这不起作用,因为一切似乎都是正确的。如果你们能找出问题,那将非常有帮助。谢谢!!

4

2 回答 2

2

这告诉您该目录C:\cache不存在。该目录必须存在,您才能将文件写入其中。您可以手动创建它,也可以使用以下方法:

File directory = new File("C:\\cache");
directory.mkdir();
于 2012-10-17T23:52:22.717 回答
0

程序找不到您的文件夹。

于 2012-10-17T23:52:15.900 回答