I would like to be able to allow the user to create an object by inputing its information and pressing a button, without first instantiating the object in the code.
I am using serialization to save an object with data inputted by the user in a bi file, but every time I try to save a new object, it overwrites it
public void addMovie2() throws IOException {
MoviesLinkedList.add(new Movies (textField.getText(), ratings.getSelectedItem(), textField_1.getText()));
Movies movie1 = new Movies(textField.getText(), ratings.getSelectedItem(), textField_1.getText());
String Filename = "MoviesLinkedList.bin";
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(Filename));
os.writeObject(movie1);
os.close();
System.out.println("Done Saving");
}
I run this method whenever the user presses an "Add" button. This works for 1 move object, whenever I want to save more than one, it overwrites it.
Here is the code I wrote to read the object in a JTextArea:
String Filename = "MoviesLinkedList.bin";
try {
ObjectInputStream is = new ObjectInputStream(new FileInputStream(Filename));
Movies movie1 = (Movies) is.readObject();
MoviesTextArea.setText("Title: " + movie1.title + " Rating: " + movie1.rating + " Review: " + movie1.review);
is.close();
} catch (IOException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}