Browse Source

Implementing serializable to a list of person

main
Jesus 2 years ago
parent
commit
7cbde2fc9d
4 changed files with 51 additions and 18 deletions
  1. +15
    -12
      src/main/java/com/jesuspinar/miobjeto/serializable/Main.java
  2. +2
    -4
      src/main/java/com/jesuspinar/miobjeto/serializable/controller/ObjWriter.java
  3. +30
    -0
      src/main/java/com/jesuspinar/miobjeto/serializable/model/People.java
  4. +4
    -2
      src/main/java/com/jesuspinar/miobjeto/serializable/model/Person.java

+ 15
- 12
src/main/java/com/jesuspinar/miobjeto/serializable/Main.java View File

@ -3,27 +3,30 @@ package com.jesuspinar.miobjeto.serializable;
import com.jesuspinar.miobjeto.serializable.controller.ObjReader; import com.jesuspinar.miobjeto.serializable.controller.ObjReader;
import com.jesuspinar.miobjeto.serializable.controller.ObjWriter; import com.jesuspinar.miobjeto.serializable.controller.ObjWriter;
import com.jesuspinar.miobjeto.serializable.model.Person; import com.jesuspinar.miobjeto.serializable.model.Person;
import com.jesuspinar.miobjeto.serializable.model.People;
import java.io.File; import java.io.File;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Person person = new Person("Manolo", 32);
Person person1 = new Person("Javier", 23);
File file = new File("person.bin"); File file = new File("person.bin");
ObjWriter.write(person,file);
ObjWriter.write(person1,file);
//Create list
People data = new People();
//Init list
data.add(new Person("Manolo", 32));
data.add(new Person("Jorge", 2));
data.add(new Person("Pepe", 89));
//machaca el ultimo que ha sido escrito con nueva entrada
//opt 1 : crear un array y serializarlo para despues leerlo
//opt 2 : crear tantos archivos como obj alla xd
boolean isWrited = ObjWriter.write(data,file);
Person p = (Person) ObjReader.read(file);
Person p2 = (Person) ObjReader.read(file);
People dat = null;
if (isWrited) {
dat = (People) ObjReader.read(file);
}
if (dat != null){
System.out.println(dat.toString());
}
System.out.println(p);
System.out.println(p2);
} }
} }

+ 2
- 4
src/main/java/com/jesuspinar/miobjeto/serializable/controller/ObjWriter.java View File

@ -1,7 +1,5 @@
package com.jesuspinar.miobjeto.serializable.controller; package com.jesuspinar.miobjeto.serializable.controller;
import com.jesuspinar.miobjeto.serializable.model.Person;
import java.io.*; import java.io.*;
public class ObjWriter { public class ObjWriter {
@ -18,9 +16,9 @@ public class ObjWriter {
} }
public static byte[] convertObjectToBytes(Object obj) { public static byte[] convertObjectToBytes(Object obj) {
ByteArrayOutputStream boas = new ByteArrayOutputStream(); ByteArrayOutputStream boas = new ByteArrayOutputStream();
try (ObjectOutputStream ois = new ObjectOutputStream(boas)
try (ObjectOutputStream objOutStream = new ObjectOutputStream(boas)
){ ){
ois.writeObject(obj);
objOutStream.writeObject(obj);
return boas.toByteArray(); return boas.toByteArray();
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();


+ 30
- 0
src/main/java/com/jesuspinar/miobjeto/serializable/model/People.java View File

@ -0,0 +1,30 @@
package com.jesuspinar.miobjeto.serializable.model;
import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
public class People implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private ArrayList<Person> data ;
public People() {
data = new ArrayList<>();
}
public boolean add(Person p){
data.add(p);
return true;
}
public boolean remove(Person p){
data.remove(p);
return true;
}
@Override
public String toString() {
return data.toString();
}
}

+ 4
- 2
src/main/java/com/jesuspinar/miobjeto/serializable/model/Person.java View File

@ -3,11 +3,13 @@ package com.jesuspinar.miobjeto.serializable.model;
import java.io.Serializable; import java.io.Serializable;
public class Person implements Serializable { public class Person implements Serializable {
private static final long serialID = 1L;
private static int CONT = 0;
private int id ;
private String name; private String name;
private int age; private int age;
public Person(String name, int age) { public Person(String name, int age) {
id = ++CONT;
this.name = name; this.name = name;
this.age = age; this.age = age;
} }
@ -30,6 +32,6 @@ public class Person implements Serializable {
@Override @Override
public String toString() { public String toString() {
return "name = " + name + " age = " + age;
return "\nID = " + id + ", NAME = " + name + ", AGE = " + age;
} }
} }

Loading…
Cancel
Save