在文件中保存和加载列表

我目前正在准备考试并正在执行以下任务:


如何将 ArrayList 传递给存储列表数据的“保存”方法和将数据传回的另一个“加载”方法?


class Person {

    private String firstname;

    private String lastname;

    private String sortname;


public Person(String firstname, String lastname) {

        this.firstname = firstname;

        this.lastname = lastname;

        updateSortname();

//getter 和 setter..


根据任务我应该使用这些方法:


public static List<Person> load(String filename) throws IOException {

        return ??;

}



public static Person load(DataInputStream in) throws IOException {

        return ??;

}



public static void save(String filename, List<Person> list) throws IOException {

}



public static void save(DataOutputStream out, Person person) throws IOException {

}



public static List<Person> unserialize(String filename) throws IOException, ClassNotFoundException {

        return ??;

}



public static void serialize(String filename, List<Person> persons) throws IOException {

}

这是应该产生以下输出的主要方法:


[威利·旺卡(WonkaWilly)、查理·巴克特(BucketCharlie)、乔爷爷(JoeGrandpa)]


[威利·旺卡(WonkaWilly)、查理·巴克特(BucketCharlie)、乔爷爷(JoeGrandpa)]


[威利·旺卡(WonkaWilly)、查理·巴克特(BucketCharlie)、乔爷爷(JoeGrandpa)]


public class PersonTest {


public static void main(String[] args) throws IOException, ClassNotFoundException {

    List<Person> persons = new ArrayList<>();

    persons.add(new Person("Willy", "Wonka"));

    persons.add(new Person("Charlie", "Bucket"));

    persons.add(new Person("Grandpa", "Joe"));

    System.out.println(persons);


    Person.save("persons.sav", persons);

    persons = Person.load("persons.sav");

    System.out.println(persons);

    Person.serialize("persons.ser", persons);

    persons = Person.unserialize("persons.ser");

    System.out.println(persons);

}

}


它应该看起来像这样。但我不知道如何为 ArrayLists 做这件事。


public static void save(String filename , Graph graph ) throws IOException{


try (ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream (new FileOutputStream (filename)))) {

out.writeObject (graph);

}

}

沧海一幻觉
浏览 91回答 2
2回答

Qyouu

由于您需要Person对象的输出,因此我们需要重写toString()类Person。[威利·旺卡(WonkaWilly)、查理·巴克特(BucketCharlie)、乔爷爷(JoeGrandpa)]class Person {//Respective Constructor, Getter & Setter methods/* Returns the string representation of Person Class.&nbsp;&nbsp;* The format of string is firstName lastName (lastNameFirstName)*/&nbsp; @Override&nbsp; public String toString() {&nbsp;&nbsp; &nbsp; return String.format(firstName + " " + lastName + "("+ lastName + firstName + ")");&nbsp;&nbsp; }&nbsp;}有许多方法可以将对象写入文件。这是与PrintWriter将对象保存到文件public static void save(String filename, List<Person> list) throws IOException {&nbsp;PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));&nbsp;for (Person person : list) {&nbsp; &nbsp; pw.println(person.toString());&nbsp; &nbsp;}&nbsp;pw.close();}或者使用序列化// 你可以使用序列化机制。要使用它,您需要执行以下操作:将Person类声明为实现Serializable:public class Person implements Serializable {&nbsp; &nbsp; ...&nbsp; @Override&nbsp; public String toString() {&nbsp;&nbsp; &nbsp; &nbsp;return String.format(firstName + " " + lastName + "("+ lastName + firstName + ")");&nbsp;&nbsp; }&nbsp;}将您的列表写入文件:public static void save(String filename, List<Person> list) throws IOException {&nbsp;FileOutputStream fos = new FileOutputStream(filename);&nbsp;ObjectOutputStream oos = new ObjectOutputStream(fos);&nbsp;oos.writeObject(list);&nbsp;oos.close();}从文件中读取列表:public static List<Person> load(String filename) throws IOException {&nbsp;FileInputStream fis = new FileInputStream(filename);&nbsp;ObjectInputStream ois = new ObjectInputStream(fis);&nbsp;List<Person> list = (List<Person>) ois.readObject();&nbsp;ois.close();&nbsp;return list;}

斯蒂芬大帝

你可以尝试这样的事情:public static void save(String filename , ArrayList<Person> persons) throws IOException{&nbsp; &nbsp; try (ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream (new FileOutputStream (filename)))) {&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < persons.size; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;out.writeObject(persons.get(i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java