java - Java中的反序列化在多种方法中无法一致地工作
问题描述
deletebook()
在该方法中创建 FileInputStream 时出现错误
。但是相同的对象创建在该
searchBook()
方法中起作用。当唯一不同的是传递给方法的参数时,有谁知道为什么代码会产生不同的结果?
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestMain {
public static final String FILENAME = "cool_file.txt";
public static void main(String[] args) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
FileOutputStream fos = null;
fos = new FileOutputStream(FILENAME);
System.out.println("Welcome to the Application");
ObjectOutputStream oos1;
int sel = 0;
TestMain t = new TestMain();
do {
System.out.println("1) addSubject");
System.out.println("2) addBook");
System.out.println("3) deleteSubject");
System.out.println("4) deleteBook");
System.out.println("5) searchBook");
System.out.println("6) searchSubject");
System.out.println("7) Exit");
System.out.println("#############");
System.out.println("Insert a selection");
Scanner s = new Scanner(System.in);
sel = s.nextInt();
switch (sel) {
case 1:
t.addSubject();
break;
case 2:
t.addBook(fos);
break;
case 3:
t.deleteSubject();
break;
case 4:
t.deleteBook(fos);
break;
case 5:
t.searchBook();
break;
case 6:
t.searchSubject();
break;
case 7:
t.exitService();
break;
default:
System.out.println("Invalid Selection");
}
} while (sel != 7);
}
public void addSubject() {
System.out.println("In addSubject"+"\n");
}
public void addBook(FileOutputStream fos) throws IOException {
System.out.println("In addBook"+"\n");
long bookId;
String title;
double price;
Integer volume;
LocalDate publishDate;
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("Enter Book ID");
Book b=new Book();
Scanner s=new Scanner(System.in);
bookId=s.nextLong();
System.out.println("Enter the Title of the Book");
title=s.next();
System.out.println("Enter the price of the Book");
price=s.nextDouble();
System.out.println("Enter the Volume of the Book");
volume=s.nextInt();
System.out.println("Enter the publishDate of the Book");
String dateStr_1=s.next();
DateTimeFormatter formatter_1=DateTimeFormatter.ofPattern("dd-MM-yyyy");
publishDate= LocalDate.parse(dateStr_1,formatter_1);
b.setBookId(bookId);
b.setTitle(title);
b.setPrice(price);
b.setVolume(volume);
b.setPublishDate(publishDate);
oos.writeObject(b);
//fos.close();
//oos.close();
}
public void deleteSubject() {
System.out.println("In deleteSubject"+"\n");
}
public void deleteBook(FileOutputStream fos) throws IOException, ClassNotFoundException {
System.out.println("In deleteBook"+"\n");
System.out.println("Enter the Book Id to be deleted");
Scanner s=new Scanner(System.in);
int bookId=s.nextInt();
ArrayList<Book> results = new ArrayList<Book>();
FileInputStream fis = null;
System.out.println(fis);
try {
fis = new FileInputStream(FILENAME);
System.out.println(fis);
while (true) {
ObjectInputStream ois = new ObjectInputStream(fis);
results.add((Book) ois.readObject());
}
} catch (EOFException ignored) {
ignored.printStackTrace();
// as expected
} finally {
if (fis != null)
fis.close();
System.out.println("Finally");
}
System.out.println("results = " + results);
for(Book b:results)
{
if(b.getBookId()==bookId)
{
results.remove(b);
}
}
File file = new File(FILENAME);
if(file.exists())
{
file.delete();
System.out.println("File Exists");
if(file.exists())
{
System.out.println("File Exists after deletion");
}
}
if(file.delete())
{
System.out.println("File deleted successfully");
}
else
{
System.out.println("Failed to delete the file");
}
/*ObjectOutputStream oos = new ObjectOutputStream(fos);
for(Book newb:results)
oos.writeObject(newb);*/
}
public void searchBook() throws ClassNotFoundException, IOException {
System.out.println("In searchBook"+"\n");
ArrayList<Book> results = new ArrayList<Book>();
FileInputStream fis = null;
try {
fis = new FileInputStream(FILENAME);
while (true) {
ObjectInputStream ois = new ObjectInputStream(fis);
results.add((Book) ois.readObject());
}
} catch (EOFException ignored) {
// as expected
} finally {
if (fis != null)
fis.close();
}
System.out.println("results = " + results);
}
public void searchSubject() {
System.out.println("In searchSubject"+"\n");
}
public void exitService() {
System.out.println("Thank You for using the service..You rare now Exitinng the service");
}
}
这是控制台:
欢迎使用应用程序 1) addSubject 2) addBook 3) deleteSubject 4) deleteBook 5) searchBook 6) searchSubject 7) 退出
####### 插入选择 2 在 addBook输入图书 ID 1 输入图书标题 ufihv 输入图书价格 1 输入图书数量 1 输入图书发布日期 01-01-2001 1) addSubject 2) addBook 3) deleteSubject 4) deleteBook 5) searchBook 6) searchSubject 7) 退出
####### 插入选择 2 在 addBook输入图书 ID 2 输入图书名称 2 输入图书价格 2 输入图书数量 2 输入图书发布日期 02-02-2002 1) addSubject 2) addBook 3) deleteSubject 4) deleteBook 5) searchBook 6) searchSubject 7) 退出
####### 插入选择 4 在 deleteBook输入要删除的Book Id 1 null java.io.FileInputStream@7106e68e java.io.EOFException finally results = [Book@1a6c5a9e, Book@37bba400] File Exists File Exists after delete 删除文件失败 1) addSubject 2) addBook 3) deleteSubject 4) deleteBook 5) searchBook 6) searchSubject 7) 退出
####### 在 java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source) 处插入选择在 java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source) 在 java.io.ObjectInputStream.readStreamHeader(Unknown Source) 在 java.io.ObjectInputStream.(Unknown Source) 在 TestMain.deleteBook(TestMain.java:124) 在 TestMain .main(TestMain.java:49)
解决方案
我不太确定,为什么要删除具有对象可序列化状态的文件。因此,当您删除具有对象可序列化状态的文件时。即使文件不存在,您也会失去一切。而且,如果您尝试以其他方法访问相同的文件,您将得到异常。
此外,当您删除特定对象时,您不会将对象的最新更新状态保存到文件(您没有序列化对象的最新状态),而是直接删除了文件。那么,从反序列化对象中删除对象有什么意义呢?
推荐阅读
- python - Count and sum numbers by name
- javascript - 如何让 IDE 了解来自 JavaScript CDN 的符号信息?
- typescript - 我无法获得 json 格式的 multitem AsyncStorage React native
- security - GPG --decrypt 无法识别 --passphrase 选项
- postgresql - Postgresql:创建一个具有唯一值不区分大小写的文本字段,citext vs nondeterministic collation
- javascript - javascript - 从 javascript 对象中查找嵌套属性
- python-3.x - 如何在 Python 上使用 SoX 获取 MP3 文件的持续时间
- javascript - 单击时将 Bootstrap Dropdown 值重置为默认值
- discord.py - 放大默认表情符号 discord.py
- r - 在 R 4.1.1 for windows 中安装 tidyverse 时出错