嘿,我正在努力使我的读写方法起作用,但是我不知道这是怎么回事
除了这种方法不能正常工作之外,我还必须在TestDiskSaving类中证明序列化可以工作。这就是为什么我将其设置为null。
有任何想法吗?
public class TestDiskSaving {
public static void main(String args[]) {
ContactList myList = new ContactList();
myList.addContact();
myList.addContact();
myList.printAll();
myList.find();
myList.write();
myList = null;
System.out.println(myList);
myList.read();
System.out.println(myList);
}
}
public class ContactList {
private ArrayList<Contact> currentContacts = new ArrayList<Contact>();
/**
* Adds a contact to the list at the next unoccupied index
*
* @author Nick
*/
public void addContact() {
currentContacts.add(newContact());
}
/**
* returns the size of the contact list
*
* @author Nick
*/
public int getSize() {
return currentContacts.size();
}
/**
* take user input and return a new contact
*/
private Contact newContact() {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the person's first name.");
String newFirstName = scan.nextLine();
System.out.println("Please enter the person's last name.");
String newLastName = scan.nextLine();
if (!newLastName.trim().equals(null) && !newLastName.trim().equals("")) {
Contact theContact = new Contact(newLastName);
theContact.setFirstName(newFirstName);
System.out.println("Please enter the person's address.");
theContact.setStreetAddress(scan.nextLine());
System.out.println("Please enter the person's email address.");
theContact.setEmailAddress(scan.nextLine());
System.out.println("Please enter the person's phone number.");
theContact.setPhoneNumber(scan.nextLine());
System.out.println("Please enter any other additional information about the person.");
theContact.setNotes(scan.nextLine());
System.out.println(newFirstName + " " + newLastName + " got stored into the contact list");
return theContact;
} else {
System.out.println("You did not enter the person's last name." + "\n"
+ "The person did not get stored into the contact list.");
return null;
}
}
/**
* Prints all contacts stored in the list, alphabetically by last name, then
* first name. (not case sensitive)
*
* @author Nick
*/
public void printAll() {
ArrayList<Contact> printList = currentContacts;
Collections.sort(printList);
for (int i = 0; i < printList.size(); i++) {
printList.get(i).printInfo();
}
}
/**
* Find contact(s) in contact list by last name
*
* @author Zhixiang
*/
public void find() {
Scanner scan = new Scanner(System.in);
String lastName;
System.out.println("Please enter a last name for search:");
lastName = scan.nextLine();
boolean found = false;
for (int i = 0; i < currentContacts.size(); i++) {
if (currentContacts.get(i).getLastName().trim().equalsIgnoreCase(lastName)) {
currentContacts.get(i).printInfo();
found = true;
}
}
if (!found) {
System.out.println("Sorry. Contact with last name: " + lastName + " not found.");
}
}
/**
* Saves the current list of contacts onto the disk
*
* @author Arman
*/
public void write() {
FileOutputStream outFile;
ObjectOutputStream outObject;
try {
outFile = new FileOutputStream("data");
outObject = new ObjectOutputStream(outFile);
outObject.writeObject(currentContacts);
outObject.writeObject(currentContacts);
outFile.close();
outObject.close();
} catch (IOException ioe) {
System.out.println("Error writing objects to the file: " + ioe.getMessage());
}
}
/**
* Reads any saved data from the disk into memory, and stores it in the
* currentContacts
*
* @author Arman
*/
public void read() {
FileInputStream inFile;
ObjectInputStream inObject;
try {
inFile = new FileInputStream("data");
inObject = new ObjectInputStream(inFile);
currentContacts = (ArrayList<Contact>) inObject.readObject();
currentContacts = (ArrayList<Contact>) inObject.readObject();
inFile.close();
inObject.close();
} catch (IOException ioe) {
System.out.println("Error reading from the file: " + ioe.getMessage());
} catch (ClassNotFoundException cnfe) {
System.out.println(cnfe);
}
}
}
public class Contact implements Comparable <Contact> {
private String firstName;
private String lastName;
private String streetAddress;
private String emailAddress;
private String phoneNumber;
private String notes;
/**
* Set the last name for contact
*
* @author Arman
*/
public Contact(String lastName) {
this.lastName = lastName;
}
/**
* Return last name of the contact
* @author Zhixiang
*/
public String getLastName(){
return lastName;
}
/**
* Sets the last name of the contact
* @author Nick
*/
public void setLastName(String newLastName){
this.lastName = newLastName;
}
/**
* Prints out all data on the contact, formatted for user viewing.
* @author Zhixiang
*/
public void printInfo(){
System.out.println("Name: "+ getFirstName() + " " + getLastName());
System.out.println("Phone: "+ getPhoneNumber());
System.out.println("Address: "+ getStreetAddress());
System.out.println("Email: "+ getEmailAddress());
System.out.println("Notes: "+ getNotes());
System.out.println();
}
/**
* Returns the contact's first name
*
* @author Nick
*/
public String getFirstName() {
return firstName;
}
/**
* Assigns the contact's first name
*
* @author Nick
*/
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
/**
* Returns the contact's street address
*
* @author Nick
*/
public String getStreetAddress() {
return streetAddress;
}
/**
* Assigns the contact's street address
*
* @author Nick
*/
public void setStreetAddress(String newStreetAddress) {
streetAddress = newStreetAddress;
}
/**
* Returns the contact's email address
*
* @author Nick
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* Assigns the contact's email address
*
* @author Nick
*/
public void setEmailAddress(String newEmailAddress) {
this.emailAddress = newEmailAddress;
}
/**
* Returns the contact's phone number
*
* @author Nick
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* Assigns the contact's phone number
*
* @author Nick
*/
public void setPhoneNumber(String newPhoneNumber) {
this.phoneNumber = newPhoneNumber;
}
/**
* Returns the contact's notes
*
* @author Nick
*/
public String getNotes() {
return notes;
}
/**
* Assigns the contact's notes
*
* @author Nick
*/
public void setNotes(String newNotes) {
this.notes = newNotes;
}
/**
* Return formatted entire contact information
* @author Arman
*/
public String toString(){
return firstName + " " + lastName + " " + streetAddress + " " + emailAddress + " " + phoneNumber + " " + notes ;
}
/**
* Allow making comparison between contacts for sorting
* Compares this contact to the contact you specify. Compares by last name then first name alphabetically.
* Returns -1 if this contact comes before the other, returns 0 if they are exactly the same name, returns 1 if this comes after the other one.
* @author Nick
*/
public int compareTo(Contact otherContact){
if (this.lastName.toLowerCase().compareTo(otherContact.lastName.toLowerCase()) <= -1) {
return -1;
}
else if (this.lastName.toLowerCase().compareTo(otherContact.lastName.toLowerCase()) >= 1){
return 1;
}
else if (this.firstName.toLowerCase().compareTo(otherContact.firstName.toLowerCase()) <= -1){
return -1;
}
else if (this.firstName.toLowerCase().compareTo(otherContact.firstName.toLowerCase()) >= 1){
return 1;
}
else return 0;
}
}
最佳答案
问题是您的Contact
类未实现Serializable
。
关于java - java.io.NotSerializableException-ContactList程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44732544/