每当我最初写入文件时,一切都很好。当我将数据读入链表时,一切都很好。最后,当我保存文件时,文件内容与我刚刚写入它的初始文件匹配。
我遇到的问题是,当我尝试从保存的文件中读取对象时(第二次运行程序后(,文件中的所有内容都被擦除,我无法读取任何对象。
public class MedicalInformation {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException, EOFException {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter the file path");
String filePath = userInput.next(); //"C:\Users\ag\Desktop\medicalData\patient.dat";
System.out.println("The file's path is " + filePath);
System.out.println("Enter the file name and its path you want to read in");
// create objects that allow one to read and write to the file
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filePath));
ObjectInputStream in = new ObjectInputStream(new FileInputStream(filePath));
//creates the objects to be written into the file
Patient p1 = new Patient("Alex", "1123 metropolitan Queens NY 11385", new Date("11/20/1997"));
p1.setFirstVisit(new Date("11/20/1997"));
p1.setHeight(72);
p1.setLastVisit(new Date("11/20/1997"));
p1.setWeight(200);
out.writeObject(p1);
System.out.println(p1);
Patient p2 = new Patient("John", "200 avenue of americas Manhttann 10111", new Date("12/20/1999"));
p2.setFirstVisit(new Date("11/11/2005"));
p2.setHeight(5);
p2.setLastVisit(new Date("11/21/2010"));
p2.setWeight(150);
out.writeObject(p2);
Patient p3 = new Patient("Sarah", "24 Park avenue Manhttann 10111", new Date("09/07/1960"));
p3.setFirstVisit(new Date("05/11/1977"));
p3.setHeight(75);
p3.setLastVisit(new Date("01/21/2017"));
p3.setWeight(110);
out.writeObject(p3);
Patient p4 = new Patient("Malcolm", "56street East NY 10444", new Date("05/28/1977"));
p4.setFirstVisit(new Date("01/11/1990"));
p4.setHeight(75);
p4.setLastVisit(new Date("8/21/2016"));
p4.setWeight(155);
out.writeObject(p4);
Patient p5 = new Patient("Johnny", "89 street Jamaica 12221", new Date("12/25/1944"));
p5.setFirstVisit(new Date("03/16/1966"));
p5.setHeight(55);
p5.setLastVisit(new Date("5/21/2015"));
p5.setWeight(355);
out.writeObject(p5);
Patient p6 = new Patient("Laura", "east 4th street Brooklyn 12214", new Date("08/08/1988"));
p6.setFirstVisit(new Date("05/11/1999"));
p6.setHeight(48);
p6.setLastVisit(new Date("8/21/2012"));
p6.setWeight(350);
out.writeObject(p6);
//put them into a list
List < Patient > list = new List < Patient > ();
boolean done = false;
while (!done) {
try {
Patient patients = (Patient) in .readObject();
list.add(patients);
//an EOFException is thrown when the stream ends, which means the end of the file is reached
} catch (EOFException e) {
done = true;
out.close(); in .close();
}
}
if (option == 8) {
System.out.println("Would you like to save the file y/n");
saveFile = userInput.next();
if (saveFile.equals("y")) {
//save file
System.out.println("Enter the file's name where it is to be saved");
filePath = userInput.next();
out = new ObjectOutputStream(new FileOutputStream(filePath));
LLNode < Patient > p = list.getList();
while (p != null) {
out.writeUnshared(p.getInfo());
p = p.getLink();
}
} else {
System.out.println("You chose not to save the file");
}
out.close(); in .close();
break;
}
}
}
}
所以我要做的是输入文件名C:\Users\ag\Desktop\medicalData\patient.dat然后六个对象被写入其中。
然后我将它们读入链表
我不执行任何操作
我将链表保存到文件 C:\Users\ag\Desktop\medicalData\patient.dat
我注释掉了将 6 个对象写入文件,现在我只想阅读它们。这就是我的问题所在,我最终留下了一个包含两个字符的文件,并且无法获得 EOFException。
序列化同一对象将反序列化同一对象,即使您在发送端更改了它。您需要使用以下之一:
-
ObjectOutputStream.reset()
-
ObjectOutputStream.writeUnshared()
-
ObjectInputStream.readUnshared()
.