|
|
|
@ -1,6 +1,9 @@
|
|
|
|
package _7._3;
|
|
|
|
package _7._3;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
@ -9,29 +12,37 @@ class Person implements Serializable {
|
|
|
|
private String lastname;
|
|
|
|
private String lastname;
|
|
|
|
private transient String sortname;
|
|
|
|
private transient String sortname;
|
|
|
|
|
|
|
|
|
|
|
|
public Person() { }
|
|
|
|
public Person() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Person(String firstname, String lastname) {
|
|
|
|
public Person(String firstname, String lastname) {
|
|
|
|
this.firstname = firstname;
|
|
|
|
this.firstname = firstname;
|
|
|
|
this.lastname = lastname;
|
|
|
|
this.lastname = lastname;
|
|
|
|
updateSortname();
|
|
|
|
updateSortname();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getFirstname() {
|
|
|
|
public String getFirstname() {
|
|
|
|
return firstname;
|
|
|
|
return firstname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setFirstname(String firstname) {
|
|
|
|
public void setFirstname(String firstname) {
|
|
|
|
this.firstname = firstname;
|
|
|
|
this.firstname = firstname;
|
|
|
|
updateSortname();
|
|
|
|
updateSortname();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getLastname() {
|
|
|
|
public String getLastname() {
|
|
|
|
return lastname;
|
|
|
|
return lastname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setLastname(String lastname) {
|
|
|
|
public void setLastname(String lastname) {
|
|
|
|
this.lastname = lastname;
|
|
|
|
this.lastname = lastname;
|
|
|
|
updateSortname();
|
|
|
|
updateSortname();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getSortname() {
|
|
|
|
public String getSortname() {
|
|
|
|
return sortname;
|
|
|
|
return sortname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void updateSortname() {
|
|
|
|
public void updateSortname() {
|
|
|
|
sortname = lastname + firstname;
|
|
|
|
sortname = lastname + firstname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -43,30 +54,34 @@ class Person implements Serializable {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Person> load(String filename) throws IOException {
|
|
|
|
public static List<Person> load(String filename) throws IOException {
|
|
|
|
List<Person> people = new ArrayList<>();
|
|
|
|
try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)))) {
|
|
|
|
try (DataInputStream in = new DataInputStream(new BufferedInputStream(
|
|
|
|
return loadAll(in);
|
|
|
|
new FileInputStream(filename)))) {
|
|
|
|
|
|
|
|
boolean nextExists = in.readBoolean();
|
|
|
|
|
|
|
|
while (nextExists) {
|
|
|
|
|
|
|
|
people.add(load(in));
|
|
|
|
|
|
|
|
nextExists = in.readBoolean();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return people;
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Person> loadAll(DataInputStream in) throws IOException {
|
|
|
|
|
|
|
|
int size = in.readInt();
|
|
|
|
|
|
|
|
List<Person> persons = new ArrayList<>();
|
|
|
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
|
|
|
persons.add(load(in));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return persons;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Person load(DataInputStream in) throws IOException {
|
|
|
|
public static Person load(DataInputStream in) throws IOException {
|
|
|
|
return new Person(in.readUTF(),in.readUTF());
|
|
|
|
return new Person(in.readUTF(), in.readUTF());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void save(String filename, List<Person> list) throws IOException {
|
|
|
|
public static void save(String filename, List<Person> list) throws IOException {
|
|
|
|
try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)))) {
|
|
|
|
try(DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)))) {
|
|
|
|
for (Person p : list) {
|
|
|
|
saveAll(out, list);
|
|
|
|
out.writeBoolean(true);
|
|
|
|
|
|
|
|
save(out, p);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.writeBoolean(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void saveAll(DataOutputStream out, List<Person> persons) throws IOException {
|
|
|
|
|
|
|
|
out.writeInt(persons.size());
|
|
|
|
|
|
|
|
for (Person person : persons) {
|
|
|
|
|
|
|
|
save(out, person);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -78,16 +93,13 @@ class Person implements Serializable {
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static List<Person> unserialize(String filename) throws IOException, ClassNotFoundException {
|
|
|
|
public static List<Person> unserialize(String filename) throws IOException, ClassNotFoundException {
|
|
|
|
List<Person> people;
|
|
|
|
|
|
|
|
try (ObjectInputStream in = new ObjectInputStream(
|
|
|
|
try (ObjectInputStream in = new ObjectInputStream(
|
|
|
|
new BufferedInputStream(new FileInputStream(filename)))) {
|
|
|
|
new BufferedInputStream(new FileInputStream(filename)))) {
|
|
|
|
people = (List<Person>) in.readObject();
|
|
|
|
List<Person> people = (List<Person>) in.readObject();
|
|
|
|
for (Person p : people) {
|
|
|
|
people.forEach(Person::updateSortname);
|
|
|
|
p.updateSortname();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return people;
|
|
|
|
return people;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void serialize(String filename, List<Person> persons) throws IOException {
|
|
|
|
public static void serialize(String filename, List<Person> persons) throws IOException {
|
|
|
|
try (ObjectOutputStream out = new ObjectOutputStream(
|
|
|
|
try (ObjectOutputStream out = new ObjectOutputStream(
|
|
|
|
|