0

我正在尝试将用户输入保存在我引用的名为“contacts.txt”的文件中。我必须收集用户输入并将它们存储为电话簿格式,然后允许用户呼叫他们保存在此文件中的信息。我无法将输入写入“contacts.txt”文件。this 引用的两个函数如下所示是doSave()doAddEntry()

我已经为此工作了几个小时并且遇到了一堵砖墙,并且已经运行了数百万次调试(可能是错误的)以查看这一切都出错了......

任何人都可以提供任何可以让我走上正轨的见解吗?

<已解决>

4

1 回答 1

0

您应该Entry创建一个Serializable类并使用Serialization将数组保存Entry objects在文件中,并deserialization从保存在文件中的字节流中获取对象数组。例如,考虑下面给出的代码。这Person是一个实现java.io.Serializable marker interface. SerializeArray 在数组的主体中Person创建并Persons.ser使用saveArray. 然后使用 . 从文件中读回该数组loadArray

import java.io.*;
class Person implements Serializable //Make Person class Serializable so that its objects can be serialized.
{
    private static final long SerialVersionUID = 20L;
    String name;
    int age;
    public Person(String name,int age)
    {
        this.name = name;
        this.age = age;
    }
    public String toString()
    {
        return "Name:"+name+", Age:"+age;
    }
}
public class SerializeArray 
{
    public static void saveArray(Person[] arr)throws Exception //writes the array of Person to a file "Persons.ser"
    {
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Persons.ser"));
        os.writeObject(arr);
        os.close();
    }
    public static Person[] loadArray()throws Exception //Reads the array of Persons back from file.
    {
        ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Persons.ser"));
        Person[] arr = (Person[]) oin.readObject();
        oin.close();
        return arr;
    }
    public static void main(String[] args) throws Exception
    {
        Person[] arr= new Person[3];
        for (int i = 0 ; i < arr.length ; i++)
        {
            arr[i] = new Person("Person"+i,25+i);
        }
        System.out.println("Saving array to file");
        saveArray(arr);
        System.out.println("Reading array back from file");
        Person[] per = loadArray();
        for (Person p : per)
        {
            System.out.println(p);
        }
    }
}

更新
要将这个概念合并到您的程序中,您应该首先将您的 Entry 类声明更改为如下所示:

public class Entry implements java.io.Serializable

然后改变你的doSave()方法如下:

public void doSave() throws Exception {
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("contacts.txt"));
        os.writeObject(entryList);
        os.close();
    }

然后改变你的doLoad()方法如下:

public void doLoad() throws Exception {
    if (length != 0) {
        //throw new Exception ("I'm not empty"); 
        //File c = new File ("contacts.txt");//No need to access the File here..
        Scanner input = new Scanner(c);
        while (input.hasNextLine()) {
            Entry e = new Entry();
            e.name = input.next();
            if ("q".equalsIgnorecase(e.name))
            break;
            e.number = input.next();
            e.notes = input.next();
            doAddEntry(e);
        }
        doAddEntry(null);
    }   
}

doAddEntry并按如下方式更改您的方法:

public void doAddEntry(Entry entry) throws Exception {
    if (entry == null)
    {
      //save file here..
      doSave(); 
    }
    else if (length == 200) {
    //save file here..
    doSave(); 
    throw new Exception("I'm full");
   }
   else
   {
    boolean matched = false;
    for (int i = 0; i<length; i++) {
    if (entryList[i].name.compareToIgnoreCase(entry.name) == 0) {
      matched = true;
      break;
    }
    if(!matched)
    {
      entryList[entryList.length] = entry;
    }
   }
}

}

于 2013-04-03T19:28:01.390 回答