Loading an array of objects using Java Serialization - java

The following code is two methods, one for saving to a file using object serialization and one for loading and deserializing the saved file for the user to read:
private void SaveDeck() throws Exception {
ObjectOutputStream oos = null;
FileOutputStream fout = null;
try{
fout = new FileOutputStream(filePath, true);
oos = new ObjectOutputStream(fout);
oos.writeObject(theDeck);
} catch (Exception ex) {
ex.printStackTrace();
}finally {
if(oos != null){
oos.close();
}
}
}
private FlashCardDeck[] loadDeck(){
user.setDeckMade(true);
try {
FileInputStream fis = new FileInputStream(filePath);
ObjectInputStream in = new ObjectInputStream(fis);
this.theDeck = (FlashCardDeck[])in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e);
}
return theDeck;
}
The error I'm getting is on the load method:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: myPackage.UserInterface
Saving works fine; I've opened up the .ser file after the SaveDeck method has executed and everything seemed to check out properly.
My question is if the problem is with the file itself, the save method, or external methods? I have made sure that everything not serializable (Namely, the Scanner class) is transient.
package myPackage.FlashCards;
import java.io.Serializable;
public class FlashCardDeck implements Serializable{
private static final long serialVersionUID = -1176413113990886560L;
public FlashCard[] theDeck;
public String deckName;
public FlashCardDeck(int cards, String name) {
this.deckName = name;
theDeck = new FlashCard[cards];
for (int i = 0; i < theDeck.length; i++) {
theDeck[i] = new FlashCard(i);
}
}
public String getQuestion(int i) {
return theDeck[i].QuestionToString();
}
public String getAnswer(int i ) {
return theDeck[i].AnswerToString();
}
public String getName() {
return deckName;
}
public int getDeckSize() {
return theDeck.length;
}
}
package myPackage.FlashCards;
import java.io.Serializable;
import java.util.Scanner;
public class FlashCard implements Serializable{
private static final long serialVersionUID = -8880816241107858648L;
private transient Scanner in = new Scanner(System.in);
String question;
String answer;
public FlashCard(int i) {
setCard(i);
}
public void setCard(int cards) {
System.out.println("What is the question for number " + (cards + 1) + "?");
question = in.nextLine();
System.out.println("What is the answer for number " + (cards + 1) + "?");
answer = in.nextLine();
}
public String QuestionToString() {
return "Question: " + question;
}
public String AnswerToString() {
return "Answer: " + answer;
}
}
package myPackage.FlashCards;
import java.io.Serializable;
import java.util.InputMismatchException;
import java.util.Scanner;
public class UserInterface implements Serializable{
private static final long serialVersionUID = 7755668511730129821L;
private int moreThanOnce = 0;
boolean deckMade = false;
private transient Scanner in = new Scanner(System.in);
public int AmountOfDecks() {
int decks;
System.out.println("How many decks will you be creating? (Type the number,
not the word. Ex: 2)");
decks = in.nextInt();
while (decks <= 0) {
System.out.println("You can't have less than one deck! Try again.");
decks = in.nextInt();
}
return decks;
}
public int StartMenu() {
int choice = 0;
moreThanOnce++;
if (moreThanOnce > 1) {
choice = SecondMenu();
} else {
System.out.println("\nFlash Card Creation Engine Ver. 2.5 ALPHA");
System.out.println("Press the cooresponding number for your
choice.");
System.out.println("1. Make a deck of flash cards");
System.out.println("2. Play flash cards");
System.out.println("3. Quit \n");
try { choice = in.nextInt(); } catch (InputMismatchException ime) {}
}
return choice;
}
public int AmountOfCards(int cards) {
int catchMe;
deckMade = true;
System.out.println("How many cards would you like? (Type the number, not the
word. Ex: 2)");
try {
catchMe = in.nextInt();
while (catchMe <= 0) {
System.out.println("You can't have less than one card! Try
again!");
catchMe = in.nextInt();
}
} catch (Exception ime) {
System.out.println("Uh-oh, you did that wrong! Let's try that again.
Try typing: 3");
cards = 0;
catchMe = in.nextInt();
}
cards = catchMe;
return cards;
}
public boolean getDeckMade() {
return deckMade;
}
public void setDeckMade(boolean makeDeckMade) {
this.deckMade = makeDeckMade;
}
public String NameOfDeck() {
String name;
System.out.println("What would you like to name this deck?");
name = in.next();
return name;
}
private int SecondMenu() {
int choice = 0;
System.out.println("Now what would you like to do?");
if (deckMade) {
System.out.println("1. Make or load a deck of flash cards -- DONE");
} else {
System.out.println("1. Make a deck of flash cards.");
}
System.out.println("2. Play flash cards");
System.out.println("3. Quit \n");
try { choice = in.nextInt(); } catch (InputMismatchException ime) {}
return choice;
}
public boolean SetMode() {
boolean timed = false;
int userChoice = 0;
while (userChoice < 1 || userChoice > 2) {
System.out.println("What mode are you selecting?");
System.out.println("1. Timed");
System.out.println("2. Normal");
System.out.println("3. Help");
System.out.println("4. Quit");
userChoice = in.nextInt();
if (userChoice == 1) {
timed = true;
} else if (userChoice == 3) {
System.out.println("Timed: Answers to a flash card will
appear after a set amount of seconds, then show the next
question after the same amount of seconds, which are set by
the user (that's you!)");
System.out.println("Normal: Answers to a flash card will
appear when the user (also you!) presses enter, and wait for
enter to be pressed before showing the next question.");
} else if (userChoice == 4) {
System.out.println("Have a good day.");
System.exit(0);
} else {
System.out.println("Choose from the proivded list -- 1 for
Timed mode, 2 for Normal mode, 3 to show the Help menu, 4 to
quit.");
System.out.println();
}
}
return timed;
}
public String setQuestion(int cards) {
String question = "";
return question;
}
public String setAnswer(int cards) {
String answer = "";
return answer;
}
}

The class you're trying to serialize (and any non-transient objects referenced by that class) must implement the Serializable interface.
judging by the error you have a UserInterface class referenced there which is not serializable.
Edit:
Also
new FileOutputStream(filePath, true);
always appends to the end of the file instead of clearing the file. You may have older data in the file that is not deserialized correctly. You could try removing the file and trying again.
In general - appending different objects to a file may be a bad choice considering data corruption. If different files for each deck are not an option, I might go for a separate DeckStore object that holds all the decks and gets serialized as a whole.

Class that you want to serialize should implement Serializable interface
public class FlashCardDeck implements Serializable {
// Fields and methods of the class ...
}
The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

Related

java.util.ConcurrentModificationException Error while trying to use iterator

I am writing a program where I create an ArrayList, and I want to traverse the list with an iterator:
ArrayList<Person> flightAttendants = new ArrayList<Person>();
Iterator<Person> itr = flightAttendants.iterator();
Here is how I am trying to traverse the elements of the arraylist:
I have defined a toString method too:
while(itr.hasNext())
{
System.out.println(itr.next());
}
public String toString()
{
System.out.println("name of the passenger : "+name);
System.out.println("Age of the passenger : "+age);
System.out.println("Seat number of the passenger : "+seatNumber);
return "\n";
}
Whenever I try to run it, it gives me the error: java.util.ConcurrentModificationException
Where is the error here?
Update: here is the full code:
import java.util.*;
import java.io.*;
class Person
{
Integer age;
String name;
String seatNumber;
Integer fare;
int pnr;
Person()
{
try
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of the passenger");
name = b.readLine();
System.out.println("Enter the age of the passenger");
age = Integer.parseInt(b.readLine());
System.out.println("Enter the Seat Number you want");
seatNumber = b.readLine();
pnr = (int)(Math.random()*100000000);
System.out.println("PNR number of the passenger is : "+pnr);
}
catch(Exception e)
{
System.out.println("");
}
}
public String toString()
{
System.out.println("name of the passenger : "+name);
System.out.println("Age of the passenger : "+age);
System.out.println("Seat number of the passenger : "+seatNumber);
return "\n";
}
}
class EconomyPassenger extends Person
{
}
class BusinessPassenger extends Person
{
}
class Crew extends Person
{
}
public class Airline
{
public static void main(String[] args)
{
ArrayList<Person> flightAttendants = new ArrayList<Person>();
Iterator<Person> itr = flightAttendants.iterator();
while(true)
{
try
{
System.out.println("Welcome to Indigo!!!");
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your Choice");
System.out.println("1.Book Tickets");
System.out.println("2.Check Reservation");
System.out.println("3.Update Tickets");
System.out.println("4.Exit");
int choice=Integer.parseInt(b.readLine());
if(choice<0 || choice>4)
{
throw new InvalidChoiceException("Enter a valid choice between 1 and 4");
}
switch(choice)
{
case 1: System.out.println("\n\n1.Economy*******2.Business*******3.Crew Login*******4.Exit");
// BufferedReader c = new BufferedReader(new InputStreamReader(System.in));
int c = Integer.parseInt(b.readLine());
if(c==1)
{
flightAttendants.add(new EconomyPassenger());
}
else if(c==2)
{
flightAttendants.add(new BusinessPassenger());
}
else if(c==3)
{
flightAttendants.add(new Crew());
}
else if(c==4)
{
break;
}
break;
case 2: // System.out.println("Enter your PNR Number : ");
// int p = Integer.parseInt(b.readLine());
// System.out.println(p);
while(itr.hasNext())
{
System.out.println(itr.next());
}
break;
case 3: System.out.println("case 3");break;
case 4: return;
default: System.out.println("default");
}
}
catch(InvalidChoiceException ic)
{
// System.out.println(ic);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class InvalidChoiceException extends Exception
{
InvalidChoiceException()
{}
InvalidChoiceException(String msg)
{
System.out.println(msg);
}
}
The code being provided by you should work fine as I have tried it. Unless you show there is something else your code is working upon we cant further try to solve. I suggest you the check the question Iterators and the concurrentmodificationexception also for better understanding that your code may be somewhere falling into errors mentioned there.
As mentioned by Andrew bring the iterator into your while loop and thats working fine check now. I tried it.

Why does my recursive method make my program throw an InvalidClassException?

In my program, I have a FacebookUser class that has an ArrayList of all of that user's friends. All of those friends may also have friends. I am using recursion to get a new ArrayList that contains all of the user's friends and all of the user's friends' friends and so on. I also serialized the FacebookUser class so I can save the data. Without the new recursive method, I can serialize without error and the data persists over executions. With the recursive method, though, I get an InvalidClassException. It shows that the exception is at the line that uses the ObjectInputStream to get the FacebookUser from memory.
I'm not understanding why the new method is causing the exception to be thrown from another method. I believe the problem lies only in the FacebookUser.java class (since getRecommendations is recursive method) but I thought it'd be best to also show the UserAccount.java class since the former extends the latter. Any help will be appreciated. Thank you.
Execution:
Exception in thread "main" java.io.InvalidClassException: FacebookUser; local class incompatible: stream classdesc serialVersionUID = 4110981517967863740, local class serialVersionUID = 5032562666539812166
at java.base/java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.base/java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.base/java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectInputStream.readObject0(Unknown Source)
at java.base/java.io.ObjectInputStream.readObject(Unknown Source)
at FacebookUser.readFile(FacebookUser.java:157)
at DriverClass.main(DriverClass.java:12)
FacebookUser.Java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class FacebookUser extends UserAccount implements Serializable{
private String passwordHint;
private ArrayList<FacebookUser> friends;
public FacebookUser(String username, String password) {
super(username, password);
friends = new ArrayList<FacebookUser>();
}
#Override
public void getPasswordHelp() {
System.out.println("Password Hint: " + passwordHint);
}
public void getPasswordHelp(String username) {
if (friends.size() == 0) {
System.out.println("There is no user with that username.");
} else {
for (int i = 0; i < friends.size(); i++) {
if (friends.get(i).toString().equals(username)) {
friends.get(i).getPasswordHelp();
break;
}
if (i == friends.size() - 1) {
System.out.println("There is no user with that username.");
break;
}
}
}
}
void setPasswordHint(String hint) {
passwordHint = hint;
}
void friend(FacebookUser newFriend) {
if (friends.contains(newFriend)) {
System.out.println("That person is already your friend.");
} else {
friends.add(newFriend);
}
}
void friend(String username) {
Scanner s = new Scanner(System.in);
if (friends.size() == 0) {
System.out.println("Please create a password: ");
String password = s.nextLine();
System.out.println("Please create a password hint: ");
String passHint = s.nextLine();
FacebookUser fbu = new FacebookUser(username, password);
fbu.setPasswordHint(passHint);
friends.add(fbu);
} else {
for (int i = 0; i < friends.size(); i++) {
if (friends.get(i).toString().equals(username)) {
System.out.println("That person is already your friend.");
break;
}
if (i == friends.size() - 1) {
System.out.println("Please create a password: ");
String password = s.nextLine();
System.out.println("Please create a password hint: ");
String passHint = s.nextLine();
FacebookUser fbu = new FacebookUser(username, password);
fbu.setPasswordHint(passHint);
friends.add(fbu);
break;
}
}
}
}
void defriend(FacebookUser formerFriend) {
if (friends.contains(formerFriend)) {
friends.remove(formerFriend);
} else {
System.out.println("That person is not your friend.");
}
}
void defriend(String username) {
Scanner s = new Scanner(System.in);
if (friends.size() == 0) {
System.out.println("That person is not your friend.");
} else {
for (int i = 0; i < friends.size(); i++) {
if (friends.get(i).toString().equals(username)) {
System.out.println("Password for " + username + ": ");
String passw = s.nextLine();
if (friends.get(i).getPassword().equals(passw)) {
friends.remove(i);
break;
} else {
System.out.println("Incorrect Password");
break;
}
}
if (i == friends.size() - 1) {
System.out.println("That person is not your friend.");
break;
}
}
}
}
ArrayList<FacebookUser> getRecommendations(FacebookUser friend) {
ArrayList<FacebookUser> recFriends = new ArrayList<FacebookUser>();
for (int i = 0; i < friend.getFriends().size(); i++) {
for (int j = 0; j < recFriends.size(); j++) {
for (int k = 0; k < recFriends.size(); k++) {
if (recFriends.get(j).equals(recFriends.get(k))) {
recFriends.remove(j); // This removes any duplicate friends so there won't be an infinite loop.
break;
}
}
}
recFriends.addAll(friend.getFriends().get(i).getRecommendations(friend)); // This adds the friends' friends' friends and so on into the ArrayList.
}
return recFriends;
}
ArrayList<FacebookUser> getFriends() {
ArrayList<FacebookUser> friendsCopy = new ArrayList<FacebookUser>();
for (int i = 0; i < friends.size(); i++) {
friendsCopy.add(friends.get(i));
}
Collections.sort(friendsCopy, new Comparator<FacebookUser>() {
#Override
public int compare(FacebookUser fb1, FacebookUser fb2) {
return fb1.toString().compareToIgnoreCase(fb2.toString());
}
});
return friendsCopy;
}
public static void writeToFile(FacebookUser facebookUser) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("FacebookUser.bin"));
oos.writeObject(facebookUser); // This saves the data.
}
public static FacebookUser readFile() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("FacebookUser.bin"));
FacebookUser facebookUser = (FacebookUser) ois.readObject(); // This reads in the data which is called in the DriverClass class line 12.
return facebookUser;
}
}
UserAccount.java
import java.io.Serializable;
public abstract class UserAccount implements Serializable{
private String username;
private String password;
private boolean active;
public UserAccount(String username, String password) {
this.username = username;
this.password = password;
active = true;
}
public boolean checkPassword(String password) {
if (password.equals(this.password)) {
return true;
} else {
return false;
}
}
public void deactivateAccount() {
active = false;
}
public String getPassword() {
return password;
}
public String toString() {
return username;
}
public boolean checkActive() {
if (active == true) {
return true;
} else {
return false;
}
}
public abstract void getPasswordHelp();
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserAccount other = (UserAccount) obj;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
}
DriverClass.java
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class DriverClass {
public static void main(String[] args) throws IOException, ClassNotFoundException {
int choice = 0;
String username;
FacebookUser fu0;
try {
fu0 = FacebookUser.readFile();
} catch (FileNotFoundException e) {
fu0 = new FacebookUser("Robert", "password1");
}
while (choice != 5) {
System.out.println(
"1. List Users \n2. Add a User \n3. Delete a User \n4. Get Password Hint \n5. Get Recommendations \n6. Quit");
Scanner s = new Scanner(System.in);
choice = s.nextInt();
switch (choice) {
case 1:
System.out.println(fu0.getFriends());
break;
case 2:
System.out.println("Username: ");
s.nextLine();
username = s.nextLine();
fu0.friend(username);
break;
case 3:
System.out.println("Username: ");
s.nextLine();
username = s.nextLine();
fu0.defriend(username);
break;
case 4:
System.out.println("Username: ");
s.nextLine();
username = s.nextLine();
fu0.getPasswordHelp(username);
break;
case 5:
System.out.println(fu0.getRecommendations(fu0));
break;
case 6:
FacebookUser.writeToFile(fu0);
break;
}
}
}
}
The exception suggests that the SerialVersionUID the JVM generated differs for some instances of the FacebookUser object. If you make your class implement Serializable, it is highly recommended that you explicitly declare the SerialVersionUID variable to avoid exceptions like these.
So in the FacebookUser class you should declare a variable like this:
private static final long serialVersionUID = 42L;
See this answer for more details.

Writing input from another java method to a file

I have this code, that I'm trying to take the user input from the addEntry() method and write a method to save that to file.
public class GradeBook {
private String course;
private ArrayList<Person> students = new ArrayList<Person>();
private ArrayList<GradeBookEntry> entries = new ArrayList<GradeBookEntry>();
public GradeBook(String course){
this.course = course;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public void addStudent( Person student ){
students.add(student);
}
public void addEntry(){
System.out.println("Grade which student: ");
for (int i = 0; i < students.size(); i++) {
System.out.println(i + " " + students.get(i).getName());
}
Scanner reader = new Scanner(System.in);
int studentIndex = reader.nextInt();
reader.nextLine();
System.out.println("Enter the assessment name: ");
String assessmentName = reader.nextLine();
System.out.println("Homework (1) or exam (2): ");
int entryType = reader.nextInt();
reader.nextLine();
GradeBookEntry newEntry;
// TODO: create either a Homework or Exam entry
if( entryType == 1 ){
newEntry = new HomeworkEntry(assessmentName);
}
else {
newEntry = new ExamEntry(assessmentName);
}
// TODO: add getData method to the Homework and Exam
newEntry.getData();
newEntry.setStudent(students.get(studentIndex));
entries.add(newEntry);
}
This is the method below that I created but the text file doesnt have the correct data in it, I guess I somehow need the variables from the first method like "studentIndex" "entryType" etc., can you point me in the right direction?
public void writeFile(String fileName){
try {
FileWriter writer = new FileWriter(fileName);
for (int i=0; i<entries.size(); i++){
writer.write(students.get(i).toString());
}
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Overriding toString of Person class should be the correct way to go about this.

DeSerialized Object containing ArrayList has all null values

Hopefully someone can provide some insight on this issue. I have created an instance of an object that contains an ArrayList of information (username, password, password hint). I am trying to serialize the object. It looks like it is serializing properly, but when I restart the project to deserialize, it returns null values in the ArrayList. Why is it returning null values for the ArrayList objects?
Driver Class:
import java.io.Serializable;
public class TestingDriver implements Serializable{
private static final long serialVersionUID = 12345L;
private static TestingAccount users = new TestingAccount();
public static void main(String[] args) {
int foreverLoop = 0;
users = DeSerialize.main();
while (foreverLoop < 1) {
int selection = users.displayMainMenu();
if (selection == 1) {
users.listUsers();
}
else if (selection == 2) {
users.addUser();
}
else if (selection == 3) {
users.deleteUser();
}
else if (selection == 4) {
users.getPasswordHint();
}
else if (selection == 5) {
Serialize.main(users);
System.exit(0);
}
else {
System.out.println("That option does not exist. Please try again.");
}
}
}
}
TestingUser Class (objects of this class will populate the ArrayList):
import java.io.Serializable;
public class TestingUser extends UserAccount implements Serializable, Comparable <TestingUser> {
private static final long serialVersionUID = 12345L;
public TestingUser(String username, String password, String passwordHint) {
super(username, password, passwordHint);
}
public TestingUser() {
}
#Override
public void getPasswordHelp() {
System.out.println("");
System.out.println("Password hint: " + passwordHint);
System.out.println("");
}
#Override
public int compareTo(TestingUser otherAccount) {
if (this.username.compareToIgnoreCase(otherAccount.username) < 0) {
return -1;
}
else if (this.username.compareToIgnoreCase(otherAccount.username) > 0) {
return 1;
}
else {
return 0;
}
}
}
TestingAccount class (calling this class creates an object that contains the ArrayList):
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;
public class TestingAccount implements Serializable {
private static final long serialVersionUID = 12345L;
public ArrayList<TestingUser> userList;
private String username;
private String password;
private String passwordHint;
public TestingAccount() {
userList = new ArrayList<TestingUser>();
}
public void listUsers() {
for (int i=0; i<this.userList.size(); i++) {
System.out.println(this.userList.get(i));
}
}
#SuppressWarnings("resource")
public void addUser() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a username: ");
username = input.next();
TestingUser tempAccount = new TestingUser(username, null, null);
if (this.userList.contains(tempAccount) == true) {
System.out.println("This user already exists.");
}
else {
System.out.println("Please enter a password: ");
password = input.next();
System.out.println("Please enter a password hint: ");
passwordHint = input.next();
tempAccount.password = password;
tempAccount.passwordHint = passwordHint;
this.userList.add(tempAccount);
System.out.println("Account " + tempAccount.username + " has been added.");
}
}
#SuppressWarnings("resource")
public void deleteUser() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the username to be deleted: ");
username = input.next();
TestingUser tempAccount = new TestingUser(username, null, null);
if (this.userList.contains(tempAccount) == true) {
int actIndex = this.userList.indexOf(tempAccount);
System.out.println("Please enter the password: ");
password = input.next();
tempAccount.password = password;
boolean passwordGood = this.userList.get(actIndex).CheckPassword(tempAccount);
int accountIndex = this.userList.indexOf(tempAccount);
tempAccount = this.userList.get(accountIndex);
if (passwordGood == true) {
this.userList.remove(actIndex);
System.out.println("The account has been deleted.");
}
else {
System.out.println("The password is not correct.");
}
}
else {
System.out.println("The account does not exist.");
}
}
#SuppressWarnings("resource")
public void getPasswordHint() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a username: ");
username = input.next();
TestingUser tempAccount = new TestingUser(username, null, null);
if (this.userList.contains(tempAccount) == true) {
int actIndex = this.userList.indexOf(tempAccount);
tempAccount = this.userList.get(actIndex);
System.out.println("The password hint isL: " + tempAccount.passwordHint);
}
else {
System.out.println("The account does not exist.");
}
}
#SuppressWarnings({ "resource" })
public int displayMainMenu() {
int selection = 0;
Scanner input = new Scanner(System.in);
System.out.println("");
System.out.println("System Menu:");
System.out.println("1. List Users");
System.out.println("2. Add User");
System.out.println("3. Delete User");
System.out.println("4. Get Password Hint");
System.out.println("5. Quit");
System.out.println("");
System.out.println("What would you like to do?");
selection = input.nextInt();
return selection;
}
}
Serialize class:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Serialize {
public static void main(TestingAccount users) {
try {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("serialize"));
oos.writeObject(users);
oos.close();
} catch (FileNotFoundException e1) {
System.err.println("File not found.");
} catch (IOException e2) {
System.err.println("Unable to serialize.");
e2.printStackTrace();
}
}
}
Deserialize class:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeSerialize {
public static TestingAccount main() {
TestingAccount deSerialize = null;
try {
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("serialize"));
deSerialize = (TestingAccount) ois.readObject();
ois.close();
} catch (FileNotFoundException e1) {
System.err.println("Unable to open file.");
} catch (IOException e2) {
System.err.println("Could not de-serialize.");
e2.printStackTrace();
} catch (ClassNotFoundException e3) {
System.err.println("Could not cast to class TestingAccount.");
}
return deSerialize;
}
}
It looks like UserAccount isn'tSerializable. So when you serialize the derived TestingUser class, none of the UserAccount data gets serialized. See Object Serialization Specification #2.1.13(a). TestingUser doesn't have any instance state of its own to serialize.
The solution is to make UserAccount implement Serializable.
Not sure why this comes as a surprise.

How to Serialize an ArrayLIst in java without getting errors?

I am just trying to output a previously created ArrayList to serialise it for future storage.
but when I attmept to do so I get the runTime error "notSerialisableException: Department.
Is their a speicial way of serializing an arrayList??
Would someone be able to tell me why I may be getting this error.
This is the code:
import java.awt.*;
import java.util.*;
import java.io.*;
import java.io.Serializable;
public class tester1ArrayListObjectSave
{
private ArrayList <Department> allDeps = new ArrayList<Department>();
private int choice = 0;
private String name;
private String loc;
Department theDepartment;
Scanner scan;
public static void main(String[] args)
{
new tester1ArrayListObjectSave();
}
public tester1ArrayListObjectSave()
{
scan = new Scanner(System.in);
options();
}
public void options()
{
System.out.println("wadya wanna do");
System.out.println("1. create a new department");
System.out.println("2. read from text file");
System.out.println("4. save it to system as a serializable file");
System.out.println(". read from text file");
System.out.println("3. to exit");
choice = scan.nextInt();
workOutOptions();
}
public void workOutOptions()
{
if (choice ==1)
{
createNewEmp();
}
else if (choice ==2)
{
try
{
readTextToSystem();
}
catch (IOException exc)
{
System.out.println("uh oh their was an error: "+exc);
}
}
else if (choice == 3)
{
System.exit(0);
}
else if (choice ==4)
{
try
{
createSerialisable();
}
catch (IOException exc)
{
System.out.println("sorry could not serialise data cause of this:"+exc);
}
}
else
{
System.out.println("do nothing");
}
}
public void createNewEmp()
{
System.out.println("What is the name");
name = scan.next();
System.out.println("what is the chaps loc");
loc = scan.next();
try
{
saveToSystem();
}
catch (IOException exc)
{
// do something here to deal with problems
}
theDepartment = new Department(name,loc);
allDeps.add(theDepartment);
options();
}
public void saveToSystem() throws IOException
{
FileOutputStream fos = new FileOutputStream( "backUp.txt", true );
PrintStream outFile = new PrintStream(fos);
System.out.println("added to system succesfully");
outFile.println(name);
outFile.println(loc);
outFile.close();
options();
}
public void readTextToSystem() throws IOException
{
Scanner inFile = new Scanner ( new File ("backUp.txt") );
while (inFile.hasNextLine())
{
name=inFile.nextLine();
System.out.println("this is the name: "+name);
loc = inFile.nextLine();
System.out.println("this is the location: "+loc);
Department dDepartment = new Department(name,loc);
allDeps.add(dDepartment);
options();
}
System.out.println(allDeps);
}
public void createSerialisable() throws IOException
{
FileOutputStream fileOut = new FileOutputStream("theBkup.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(allDeps);
options();
}
}
ArrayList isn't the problem; your Department object is.
You need to implement the Serializable interface in that object.

Categories

Resources