parent
a5f57c0d4e
commit
67280ac2cd
@ -1,117 +1,129 @@
|
|||||||
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;
|
||||||
|
|
||||||
class Person implements Serializable {
|
class Person implements Serializable {
|
||||||
private String firstname;
|
private String firstname;
|
||||||
private String lastname;
|
private String lastname;
|
||||||
private transient String sortname;
|
private transient String sortname;
|
||||||
|
|
||||||
public Person() { }
|
public Person() {
|
||||||
public Person(String firstname, String lastname) {
|
}
|
||||||
this.firstname = firstname;
|
|
||||||
this.lastname = lastname;
|
public Person(String firstname, String lastname) {
|
||||||
updateSortname();
|
this.firstname = firstname;
|
||||||
}
|
this.lastname = lastname;
|
||||||
public String getFirstname() {
|
updateSortname();
|
||||||
return firstname;
|
}
|
||||||
}
|
|
||||||
public void setFirstname(String firstname) {
|
public String getFirstname() {
|
||||||
this.firstname = firstname;
|
return firstname;
|
||||||
updateSortname();
|
}
|
||||||
}
|
|
||||||
public String getLastname() {
|
public void setFirstname(String firstname) {
|
||||||
return lastname;
|
this.firstname = firstname;
|
||||||
}
|
updateSortname();
|
||||||
public void setLastname(String lastname) {
|
}
|
||||||
this.lastname = lastname;
|
|
||||||
updateSortname();
|
public String getLastname() {
|
||||||
}
|
return lastname;
|
||||||
public String getSortname() {
|
}
|
||||||
return sortname;
|
|
||||||
}
|
public void setLastname(String lastname) {
|
||||||
public void updateSortname() {
|
this.lastname = lastname;
|
||||||
sortname = lastname + firstname;
|
updateSortname();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public String getSortname() {
|
||||||
public String toString() {
|
return sortname;
|
||||||
return firstname + " " + lastname + " (" + sortname + ")";
|
}
|
||||||
}
|
|
||||||
|
public void updateSortname() {
|
||||||
|
sortname = lastname + firstname;
|
||||||
public static List<Person> load(String filename) throws IOException {
|
}
|
||||||
List<Person> people = new ArrayList<>();
|
|
||||||
try (DataInputStream in = new DataInputStream(new BufferedInputStream(
|
@Override
|
||||||
new FileInputStream(filename)))) {
|
public String toString() {
|
||||||
boolean nextExists = in.readBoolean();
|
return firstname + " " + lastname + " (" + sortname + ")";
|
||||||
while (nextExists) {
|
}
|
||||||
people.add(load(in));
|
|
||||||
nextExists = in.readBoolean();
|
|
||||||
}
|
public static List<Person> load(String filename) throws IOException {
|
||||||
}
|
try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)))) {
|
||||||
return people;
|
return loadAll(in);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public static Person load(DataInputStream in) throws IOException {
|
|
||||||
return new Person(in.readUTF(),in.readUTF());
|
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++) {
|
||||||
public static void save(String filename, List<Person> list) throws IOException {
|
persons.add(load(in));
|
||||||
try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)))) {
|
}
|
||||||
for (Person p : list) {
|
return persons;
|
||||||
out.writeBoolean(true);
|
}
|
||||||
save(out, p);
|
|
||||||
}
|
public static Person load(DataInputStream in) throws IOException {
|
||||||
out.writeBoolean(false);
|
return new Person(in.readUTF(), in.readUTF());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public static void save(String filename, List<Person> list) throws IOException {
|
||||||
public static void save(DataOutputStream out, Person person) throws IOException {
|
try(DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)))) {
|
||||||
out.writeUTF(person.getFirstname());
|
saveAll(out, list);
|
||||||
out.writeUTF(person.getLastname());
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void saveAll(DataOutputStream out, List<Person> persons) throws IOException {
|
||||||
@SuppressWarnings("unchecked")
|
out.writeInt(persons.size());
|
||||||
public static List<Person> unserialize(String filename) throws IOException, ClassNotFoundException {
|
for (Person person : persons) {
|
||||||
List<Person> people;
|
save(out, person);
|
||||||
try (ObjectInputStream in = new ObjectInputStream(
|
}
|
||||||
new BufferedInputStream(new FileInputStream(filename)))) {
|
}
|
||||||
people = (List<Person>) in.readObject();
|
|
||||||
for (Person p : people) {
|
public static void save(DataOutputStream out, Person person) throws IOException {
|
||||||
p.updateSortname();
|
out.writeUTF(person.getFirstname());
|
||||||
}
|
out.writeUTF(person.getLastname());
|
||||||
}
|
}
|
||||||
return people;
|
|
||||||
}
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public static void serialize(String filename, List<Person> persons) throws IOException {
|
public static List<Person> unserialize(String filename) throws IOException, ClassNotFoundException {
|
||||||
try (ObjectOutputStream out = new ObjectOutputStream(
|
try (ObjectInputStream in = new ObjectInputStream(
|
||||||
new BufferedOutputStream(new FileOutputStream(filename)))) {
|
new BufferedInputStream(new FileInputStream(filename)))) {
|
||||||
out.writeObject(persons);
|
List<Person> people = (List<Person>) in.readObject();
|
||||||
}
|
people.forEach(Person::updateSortname);
|
||||||
}
|
return people;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void serialize(String filename, List<Person> persons) throws IOException {
|
||||||
|
try (ObjectOutputStream out = new ObjectOutputStream(
|
||||||
|
new BufferedOutputStream(new FileOutputStream(filename)))) {
|
||||||
|
out.writeObject(persons);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PersonTest {
|
public class PersonTest {
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
||||||
List<Person> persons = new ArrayList<>();
|
List<Person> persons = new ArrayList<>();
|
||||||
persons.add(new Person("Willy", "Wonka"));
|
persons.add(new Person("Willy", "Wonka"));
|
||||||
persons.add(new Person("Charlie", "Bucket"));
|
persons.add(new Person("Charlie", "Bucket"));
|
||||||
persons.add(new Person("Grandpa", "Joe"));
|
persons.add(new Person("Grandpa", "Joe"));
|
||||||
System.out.println(persons);
|
System.out.println(persons);
|
||||||
|
|
||||||
Person.save("persons.sav", persons);
|
Person.save("persons.sav", persons);
|
||||||
persons = Person.load("persons.sav");
|
persons = Person.load("persons.sav");
|
||||||
System.out.println(persons);
|
System.out.println(persons);
|
||||||
Person.serialize("persons.ser", persons);
|
Person.serialize("persons.ser", persons);
|
||||||
persons = Person.unserialize("persons.ser");
|
persons = Person.unserialize("persons.ser");
|
||||||
System.out.println(persons);
|
System.out.println(persons);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue