繁体   English   中英

通过一个类将数组的所有内容保存到file.txt中,然后从同一文件加载/保存/读取?

[英]Saving all contents of array through a class into a file.txt and then loading / saving / reading from that same file?

我正在尝试将用户输入保存在一个我称为“ contacts.txt”的文件中。 我必须收集用户输入并将其以电话簿格式存储,然后允许用户调用他们已保存在此文件中的信息。 我在将输入内容写入“ contacts.txt”文件时遇到问题。 可以在下面看到此引用的两个函数: doSave()doAddEntry()

我已经在这个地方工作了几个小时,碰到了墙,还进行了数百万次调试(可能是错误地调试),以查看问题出在哪里……

谁能提供任何可以使我走上正确道路的见解?

<解决>

您应该将EntrySerializable类,并使用SerializationEntry 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如下所示更改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;
    }
   }
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM