gpt4 book ai didi

java - 将对象写入二进制文件时出错

转载 作者:行者123 更新时间:2023-11-30 04:38:04 25 4
gpt4 key购买 nike

所以,这是我的代码:

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();
}

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

最佳答案

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

File directory = new File("C:\\cache");
directory.mkdir();

关于java - 将对象写入二进制文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12945162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com