| @ -0,0 +1,10 @@ | |||||
| <?xml version="1.0" encoding="UTF-8"?> | |||||
| <classpath> | |||||
| <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"> | |||||
| <attributes> | |||||
| <attribute name="module" value="true"/> | |||||
| </attributes> | |||||
| </classpathentry> | |||||
| <classpathentry kind="src" path="src"/> | |||||
| <classpathentry kind="output" path="bin"/> | |||||
| </classpath> | |||||
| @ -0,0 +1,17 @@ | |||||
| <?xml version="1.0" encoding="UTF-8"?> | |||||
| <projectDescription> | |||||
| <name>practica8_1</name> | |||||
| <comment></comment> | |||||
| <projects> | |||||
| </projects> | |||||
| <buildSpec> | |||||
| <buildCommand> | |||||
| <name>org.eclipse.jdt.core.javabuilder</name> | |||||
| <arguments> | |||||
| </arguments> | |||||
| </buildCommand> | |||||
| </buildSpec> | |||||
| <natures> | |||||
| <nature>org.eclipse.jdt.core.javanature</nature> | |||||
| </natures> | |||||
| </projectDescription> | |||||
| @ -0,0 +1,2 @@ | |||||
| eclipse.preferences.version=1 | |||||
| encoding/<project>=UTF-8 | |||||
| @ -0,0 +1,14 @@ | |||||
| eclipse.preferences.version=1 | |||||
| org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | |||||
| org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 | |||||
| org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | |||||
| org.eclipse.jdt.core.compiler.compliance=17 | |||||
| org.eclipse.jdt.core.compiler.debug.lineNumber=generate | |||||
| org.eclipse.jdt.core.compiler.debug.localVariable=generate | |||||
| org.eclipse.jdt.core.compiler.debug.sourceFile=generate | |||||
| org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | |||||
| org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled | |||||
| org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | |||||
| org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning | |||||
| org.eclipse.jdt.core.compiler.release=enabled | |||||
| org.eclipse.jdt.core.compiler.source=17 | |||||
| @ -0,0 +1,3 @@ | |||||
| En este programa crearemos 3 personas y mostraremos sus edades y sus nombres mediante los metodos writeObject() y readObject() y los guardaremos en un archivo. | |||||
| ``` [ID = 1, Nombre = Paquito, Edad = 36, ID = 2, Nombre = Pedro, Edad = 88, ID = 3, Nombre = Juanjo, Edad = 59] ``` | |||||
| @ -0,0 +1,35 @@ | |||||
| package practica8_1; | |||||
| import java.io.Serial; | |||||
| import java.io.Serializable; | |||||
| import java.util.ArrayList; | |||||
| public class Gente implements Serializable{ | |||||
| @Serial | |||||
| private static final long serialVersionUID = 1L; | |||||
| private ArrayList<Persona> datos ; | |||||
| public Gente() { | |||||
| datos = new ArrayList<>(); | |||||
| } | |||||
| public boolean add(Persona p){ | |||||
| datos.add(p); | |||||
| return true; | |||||
| } | |||||
| public boolean remove(Persona p){ | |||||
| datos.remove(p); | |||||
| return true; | |||||
| } | |||||
| public ArrayList<Persona> getData() { | |||||
| return datos; | |||||
| } | |||||
| @Override | |||||
| public String toString() { | |||||
| return datos.toString(); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,26 @@ | |||||
| package practica8_1; | |||||
| import java.io.File; | |||||
| public class Main { | |||||
| public static void main(String[] args) { | |||||
| File file = new File("Persona.bin"); | |||||
| Gente data = new Gente(); | |||||
| data.add(new Persona("Paquito", 36 )); | |||||
| data.add(new Persona("Pedro", 88)); | |||||
| data.add(new Persona("Juanjo", 59)); | |||||
| boolean isWritten = ObjWriter.write(data,file); | |||||
| Gente g = null; | |||||
| if (isWritten) { | |||||
| g = (Gente) ObjReader.read(file); | |||||
| System.out.println(g.toString()); | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,21 @@ | |||||
| package practica8_1; | |||||
| import java.io.File; | |||||
| import java.io.FileInputStream; | |||||
| import java.io.IOException; | |||||
| import java.io.ObjectInputStream; | |||||
| public class ObjReader { | |||||
| public static Object read(File file) { | |||||
| Object result = null; | |||||
| try (FileInputStream fis = new FileInputStream(file); | |||||
| ObjectInputStream ois = new ObjectInputStream(fis) | |||||
| ){ | |||||
| result = ois.readObject(); | |||||
| } catch (IOException | ClassNotFoundException e) { | |||||
| throw new RuntimeException(e); | |||||
| } | |||||
| return result; | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,21 @@ | |||||
| package practica8_1; | |||||
| import java.io.File; | |||||
| import java.io.FileOutputStream; | |||||
| import java.io.IOException; | |||||
| import java.io.ObjectOutputStream; | |||||
| public class ObjWriter { | |||||
| public static boolean write(Object obj, File out) { | |||||
| try (FileOutputStream fos = new FileOutputStream(out); | |||||
| ObjectOutputStream oos = new ObjectOutputStream(fos) | |||||
| ){ | |||||
| oos.writeObject(obj); | |||||
| oos.flush(); | |||||
| return true; | |||||
| } catch (IOException e) { | |||||
| throw new RuntimeException(e); | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,38 @@ | |||||
| package practica8_1; | |||||
| import java.io.Serializable; | |||||
| public class Persona implements Serializable{ | |||||
| private int id ; | |||||
| private String nombre; | |||||
| private int edad; | |||||
| private static int CONT = 0; | |||||
| public Persona(String nombre, int edad) { | |||||
| id = ++CONT; | |||||
| this.nombre = nombre; | |||||
| this.edad = edad; | |||||
| } | |||||
| public int getAge() { | |||||
| return edad; | |||||
| } | |||||
| public void setAge(int age) { | |||||
| this.edad = edad; | |||||
| } | |||||
| public String getName() { | |||||
| return nombre; | |||||
| } | |||||
| public void setName(String name) { | |||||
| this.nombre = nombre; | |||||
| } | |||||
| @Override | |||||
| public String toString() { | |||||
| return "ID = " + id + ", Nombre = " + nombre + ", Edad = " + edad+""; | |||||
| } | |||||
| } | |||||