【发布时间】:2016-02-27 15:54:21
【问题描述】:
我正在尝试将一个对象输出到一个文件中,下面的代码运行良好。
val myFile = new File(myPath + "_" + myFileName)
val myData = new ObjectOutputStream(new FileOutputStream(myFile))
但是,如果我想在 myPath 下创建 myFileName :
val myFile = new File(myPath + "/" + myFileName)
val myData = new ObjectOutputStream(new FileOutputStream(myFile))
我得到了 java.io.FileNotFoundException。
知道我可能错过了什么吗?谢谢!
【问题讨论】:
-
myPath的路径可能不存在。您还可以使用File(String, String)将路径元素连接在一起。考虑使用new File(myPath).exists()之类的东西来检查路径是否存在和/或File#mkdirs来创建路径 - 请务必检查返回结果,因为路径可能无效或者您没有写入权限跨度>
标签: java scala file objectoutputstream