我想从文件中读取对象并将数据放置到LinkedList
并将引用返回给它。但是当我尝试这个方法时,它返回没有数据的LinkedList。无法从文件中读取对象并将它们放到java中的链接列表中
private static LinkedList<Course> readFromFile(String fileName)
throws FileNotFoundException, IOException {
LinkedList<Course> tmp = new LinkedList<Course>();
reader = new ObjectInputStream(new FileInputStream(fileName));
try {
LinkedList<Course> readObject2 = (LinkedList<Course>) reader
.readObject();
LinkedList<Course> readObject = readObject2;
tmp = readObject;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return tmp;
}
我的写作方法看起来像这样
private static boolean writeToFile(String fileName, LinkedList<Course> templist)
throws IOException {
LinkedList<Course> templist1 = new LinkedList<Course>();
if (createFile(fileName)) {
FileOutputStream outF = new FileOutputStream(fileName);
outO = new ObjectOutputStream(outF);
outO.writeObject(templist1);
//outO.flush();
return true;
}
else
return false;
}
写作方法看起来像这样
private static boolean writeToFile(String fileName, LinkedList<Course> templist) throws IOException {
LinkedList<Course> templist1 = new LinkedList<Course>();
if (createFile(fileName)) {
FileOutputStream outF = new FileOutputStream(fileName);
outO = new ObjectOutputStream(outF);
outO.writeObject(templist1);
//outO.flush();
return true;
}
else
return false;
}
请问您的文件的存储对象名单在里面? – SMA
你是如何序列化你的课程的?你有序列化整个列表'writeObject(LinkedListOfCourses)'还是你分别序列化每个课程'writeObject(Course1),...,writeObject(CourseX)'。 – Pshemo
要使用更多详细信息更新您的问题,请使用您的帖子下方的[[edit]]选项。 – Pshemo