对象序列化
题目描述:
现有类 Person 和 Book, 其定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 import java.io.Serializable;import java.time.LocalDate;public class Person implements Serializable { private static final long serialVersionUID = 1L ; private String name; private String gender; private LocalDate birthday; private String biography; public Person () {} public Person (String name, String gender, String biography, int year, int month, int day) { this .name = name; this .gender = gender; this .biography = biography; this .birthday = LocalDate.of(year, month, day); } public String getName () {return name;} public void setName (String name) {this .name = name;} public String getGender () {return gender;} public void setGender (String gender) {this .gender = gender;} public LocalDate getBirthday () {return birthday;} public void setBirthday (LocalDate birthday) {this .birthday = birthday;} public String getBiography () {return biography;} public void setBiography (String biography) {this .biography = biography;} @Override public String toString () { return "name: " + name + " , gender: " + gender + " , birthday: " + birthday + " , biography: " + biography; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import java.io.Serializable;public class Book implements Serializable { private static final long serialVersionUID = 1L ; private String name; private Person author; private int price; public String getName () {return name;} public void setName (String name) {this .name = name;} public Person getAuthor () {return author;} public void setAuthor (Person author) {this .author = author;} public int getPrice () {return price;} public void setPrice (int price) {this .price = price;} public Book () {} public Book (String name, Person author, int price) { this .name = name; this .author = author; this .price = price; } @Override public String toString () { return "name: " + name + "\nauthor: " + author + "\nprice: " + price; } }
有一段程序用 ObjectOutputStream
的 writeObject()
方法连续向文件 dict.dic
中写入了 5 个 Book 类型的对象. 现请你写一段程序将这 5 个对象读出来.
注意: 你的程序中要把 Person 和 Book 类的定义复制过去.
输入:
为一个整数, 只可能是 1, 2, 3, 4, 5 之一.
输出:
只有一行字符串, 为对应的文件名
输入样例:
输出样例:
1 2 3 name: Thinking in Java author: name: Bruce Eckel , gender: male , birthday: 1957-07-08 , biography: Bruce Eckel is a computer programmer, author and consultant. price: 75
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 import java.io.*;import java.time.LocalDate;import java.util.Scanner;public class Main { public static void main (String[] args) throws IOException, ClassNotFoundException { var in = new Scanner (System.in); var n = in.nextInt(); var file = new ObjectInputStream (new FileInputStream ("dict.dic" )); var books = new Book [5 ]; for (int i = 0 ; i < 5 ; i++) books[i] = (Book) file.readObject(); System.out.println(books[n - 1 ]); } }class Person implements Serializable { @Serial private static final long serialVersionUID = 1L ; private String name; private String gender; private LocalDate birthday; private String biography; public Person () {} public Person (String name, String gender, String biography, int year, int month, int day) { this .name = name; this .gender = gender; this .biography = biography; this .birthday = LocalDate.of(year, month, day); } public String getName () {return name;} public void setName (String name) {this .name = name;} public String getGender () {return gender;} public void setGender (String gender) {this .gender = gender;} public LocalDate getBirthday () {return birthday;} public void setBirthday (LocalDate birthday) {this .birthday = birthday;} public String getBiography () {return biography;} public void setBiography (String biography) {this .biography = biography;} @Override public String toString () { return "name: " + name + " , gender: " + gender + " , birthday: " + birthday + " , biography: " + biography; } }class Book implements Serializable { @Serial private static final long serialVersionUID = 1L ; private String name; private Person author; private int price; public String getName () {return name;} public void setName (String name) {this .name = name;} public Person getAuthor () {return author;} public void setAuthor (Person author) {this .author = author;} public int getPrice () {return price;} public void setPrice (int price) {this .price = price;} public Book () {} public Book (String name, Person author, int price) { this .name = name; this .author = author; this .price = price; } @Override public String toString () { return "name: " + name + "\nauthor: " + author + "\nprice: " + price; } }
2023-04-24
IP属地: 北京