我正在尝试序列化以下类:
public class Library extends ArrayList<Book> implements Serializable{
public Library(){
check();
}
Run Code Online (Sandbox Code Playgroud)
使用该类的以下方法:
void save() throws IOException {
String path = System.getProperty("user.home");
File f = new File(path + "\\Documents\\CardCat\\library.ser");
ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (f));
oos.writeObject(this);
oos.close();
}
Run Code Online (Sandbox Code Playgroud)
但是,library.ser程序正在创建一个名为"no"的目录,而不是创建一个名为的文件library.ser.为什么是这样?
如果它有用,则最初从此方法(同一类)调用save()方法:
void checkFile() {
String path = System.getProperty("user.home");
File f = new File(path + "\\Documents\\CardCat\\library.ser");
try {
if (f.exists()){
load(f);
}
else if (!f.exists()){
f.mkdirs();
save();
}
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(Library.class.getName()).log(Level.SEVERE, null, ex);
}
}
Run Code Online (Sandbox Code Playgroud)
use*_*421 34
File.mkdirs()创建目录而不是文件
这就是应该做的.阅读Javadoc.没有关于创建文件的信息.
f.mkdirs();
正是这条线创建了目录.它应该是
f.getParentFile().mkdirs();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7229 次 |
| 最近记录: |