[英]Reading line by line in a serialized file
序列化文件中沒有新行(除非您在最初編寫它時以某種方式創建了它)。 您將必須使用ObjectOutputStream
和ObjectInputStream
進行序列化和反序列化。 如果要序列化一堆對象,最好的選擇可能是序列化整個列表,然后再次反序列化整個對象 ,然后將其添加回新列表。 從另一個問題來看,一個不錯的選擇可能是將每個節點添加到列表中,而不是將每個對象分別寫入文件中:
...
if (focusNode != null){
System.out.println(focusNode);
list.add(focusNode);
preOrderTraverseTree(focusNode.leftChild);
preOrderTraverseTree(focusNode.rightChild);
}//end if
...
然后保存:
try (ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream(file))) {
output.writeObject(lList);//this will write the list as a whole to the file
}
並讀回去,我建議將其放回列表中:
try (ObjectInputStream input = new ObjectInputStream(
new FileInputStream(file))) {
newList = (List) input.readObject();
從那里開始,如果您需要再次使用這些對象,則可以將它們從列表中拉出,等等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.