无法将对象保存到dat文件

时间:2013-04-29 05:18:34

标签: java file file-io

我在CS中有一个项目,我想创建一个可以充当地址簿的GUI程序。该程序添加,查找和删除联系人。该程序还假设能够读取/保存联系人。我在阅读联系人时遇到问题。这是该部分的代码。

PhoneDirectory Contacts = new PhoneDirectory(100);
private static final String DATA_FILE = "records.dat";

private static void readRecords() {
    try {
        FileInputStream fileIn = new FileInputStream(DATA_FILE);
    try (ObjectInputStream in = new ObjectInputStream(fileIn)) {
        Contacts = in.readObject();
    }
    } 
    catch (IOException | ClassNotFoundException e) {
        JOptionPane.showMessageDialog(null, "File does not exist or cannot be read.", "Error", JOptionPane.ERROR_MESSAGE);
  } 

}

这里我创建了一个对象“contacts”,用于存储用户输入的信息。在Contacts = in.readObject();行中,我在Contacts = in.readObject();行中收到错误消息。我想我得到了这个,因为Contacts是变量而不是对象。

我应该在联系人上做些什么?

这是我项目的其余代码。

主类

package console;

public class Console {

    public static void main(String[] args)
    {
        PhoneBookGUI frame = new PhoneBookGUI();
        frame.setVisible(true);
    }
}

PhoneDirectory

package console;

import javax.swing.JOptionPane;

public class PhoneDirectory {
  private PhoneRecord[] records;
  private int numRecords;

  public PhoneDirectory(int size)
  {
      this.records = new PhoneRecord[size];
      this.numRecords = 0;
  }

    public PhoneRecord[] getRecords() {
        return records;
    }

    public int getNumRecords() {
        return numRecords;
    }

    public void searchRecords(String inputName)
    {
        int j;
        for (j=0; j<numRecords; j++) {
            if (records[j].getName().contains(inputName)){
                JOptionPane.showMessageDialog(null, "Name:" + " " +         records[j].getName() + "\nNumber:" +" " + records[j].getNumber(), inputName, JOptionPane.INFORMATION_MESSAGE);
                return;
            }
       }
    }

    public void deleteRecord(String contactName)
    {
        int i;
        for (i=0; i <numRecords;i++) {
            if (records[i].getName().equals(contactName)) {
                records[i] = records[numRecords-1];
                numRecords--;
            }
        }
    }

    public void updateRecord(String contactName, String phoneNumber)
    {
        int k;
        for (k=0; k<numRecords;k++) {
            if (records[k].getName().equals(contactName)) {
            records[k].setNumber(phoneNumber);
            break;
            }
        }
    }

    public void addRecord(PhoneRecord record)
    {
        if (numRecords < records.length) {
            records [numRecords] = record;
            numRecords++;
        }

        if (numRecords > records.length) {
            PhoneRecord [] tempArray = new PhoneRecord [records.length*2];
            for (int m=0; m<records.length; m++) {
                tempArray[m] = records[m];
            }
            records = tempArray;
        }
    }
}

PhoneRecord

    package console;

// Represents a record containing a name and a phone number
class PhoneRecord {
  private String name;
  private String number;

  // Constructor
  public PhoneRecord(String personName, String phoneNumber) {
    name = personName;                       
    number = phoneNumber;
  }

  // Returns the name stored in the record
  public String getName() {
    return name;
  }

  // Returns the phone number stored in the record
  public String getNumber() {
      return number;
  }

  public void setNumber(String num) {
      this.number = num;
  }
}

2 个答案:

答案 0 :(得分:1)

通常需要将其强制转换为更具体的对象。

PhoneDirectory Contacts = new PhoneDirectory(100);
private static final String DATA_FILE = "records.dat";

private static void readRecords() {
    try {
        FileInputStream fileIn = new FileInputStream(DATA_FILE);
    try (ObjectInputStream in = new ObjectInputStream(fileIn)) {
        Contacts = in.readObject();

应该是:

PhoneDirectory contacts = new PhoneDirectory(100);
private static final String DATA_FILE = "records.dat";

private static void readRecords() {
    try {
        FileInputStream fileIn = new FileInputStream(DATA_FILE);
    try (ObjectInputStream in = new ObjectInputStream(fileIn)) {
        contacts = (PhoneDirectory)in.readObject();  //cast the object!

答案 1 :(得分:0)

您需要让类PhoneDirectoryPhoneContant实现Serializable接口,同时在将Object文件作为Object读取时,请确保添加正确的强制转换语句。