最后一个序列化的对象将被覆盖。

huangapple go评论123阅读模式
英文:

last object serialized gets overwritten

问题

我正在编写一个程序来跟踪图书馆图书。有一个名为"book"的对象,其中包括标题、SKU编号、价格和数量。所有的图书都存储在一个ArrayList中。我试图对这些图书进行序列化并添加新的图书,但每次添加图书时,最后一个图书都会被覆盖。

以下是从保存文件中加载对象的代码:

public static void readSave() {
    File stockFile = new File("inventory.txt");
    
    try {
        if (!stockFile.createNewFile() && stockFile.length() != 0) {
            ObjectInputStream objIn = new ObjectInputStream(new FileInputStream(stockFile));
            int size = objIn.readInt();
            
            for (int i = 0; i < size; i++) {
                Book t = (Book) objIn.readObject();
                bookList.add(t);
            }
            
            objIn.close();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }	
}

以下是保存方法,它被设置为在程序执行时每次保存:

public static void save() {
    File inventoryFile = new File("inventory.txt");
    
    ObjectOutputStream objOut;
    try {
        objOut = new ObjectOutputStream(new FileOutputStream(inventoryFile));
    
        objOut.writeInt(bookList.size());
        Iterator<Book> i = bookList.iterator();
        while (i.hasNext()) {
            objOut.writeObject(i.next());
        }
    
        objOut.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

以下是调用这些函数的主方法:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                readSave();
                CampusBookWindow window = new CampusBookWindow();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
            save();
        }
    });
}
英文:

I am writing a program for keeping track of library books. There is an object book that includes a title, sku number, price and quantity. All the books are stored in an Array List. I'm trying to serialize the books and add new books but every time a book is added the last is overwritten.

here is the code below to load objects from save

public static void readSave() {
		
		File stockFile = new File(&quot;inventory.txt&quot;);
		
		try {
			if(!stockFile.createNewFile() &amp;&amp; stockFile.length() != 0) {
				ObjectInputStream objIn = new ObjectInputStream(new FileInputStream(stockFile));
				int size = objIn.readInt();
				
				for(int i = 0; i &lt; size; i++) {
					Book t = (Book) objIn.readObject();
					bookList.add(t);
				}
				
				objIn.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

here is my save method and it is set to save every time the program executes.

public static void save() {
		File inventoryFile = new File(&quot;inventory.txt&quot;);
		
		ObjectOutputStream objOut;
		try {
			objOut = new ObjectOutputStream(new FileOutputStream(inventoryFile));
		
			objOut.writeInt(bookList.size());
			Iterator&lt;Book&gt; i = bookList.iterator();
			while(i.hasNext()){
				objOut.writeObject(i.next());
			}
		
			objOut.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

below is my main method which calls these functions

public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					readSave();
					CampusBookWindow window = new CampusBookWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
				save();
			}
		});
	}

答案1

得分: 0

我的问题是,当我点击“添加书籍”按钮时,不会为该项目创建新的书籍对象,而是重新分配上一个书籍对象,因此最后一个书籍对象从书籍列表数组中被删除。

英文:

My problem was when I hit the add book button I would not create a new book object for that item, but instead I was reassigning that last book object so the object last book object was erased from the bookList array list

huangapple
  • 本文由 发表于 2020年10月22日 15:02:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64476927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定