Tôi cố gắng để serialize các lớp sau đây:File.mkdirs() tạo ra một thư mục thay vì một tập tin
public class Library extends ArrayList<Book> implements Serializable{
public Library(){
check();
}
bằng cách sử dụng phương pháp sau đây của lớp đó:
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();
}
Tuy nhiên, chứ không phải là tạo một tệp có tên là library.ser
, chương trình đang tạo thư mục có tên library.ser
không có gì trong đó. Tại sao điều này?
Nếu hữu ích của nó, save() phương pháp ban đầu được gọi là từ phương pháp này (cùng loại):
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);
}
}
Và cũng 'f.createNewFile()', để tạo tệp không tồn tại. – ddmps
@Pescis Điều đó hoàn toàn không cần thiết. 'FileOutputStream()' mới sẽ làm điều đó. – EJP
Bạn nói đúng, 'FileNotFoundException' ném nhầm lẫn tôi. – ddmps