Browse Source

first commit

master
Daniel 2 years ago
commit
f905989591
16 changed files with 187 additions and 0 deletions
  1. +10
    -0
      practica8_1/.classpath
  2. +17
    -0
      practica8_1/.project
  3. +2
    -0
      practica8_1/.settings/org.eclipse.core.resources.prefs
  4. +14
    -0
      practica8_1/.settings/org.eclipse.jdt.core.prefs
  5. BIN
      practica8_1/Persona.bin
  6. +3
    -0
      practica8_1/README.md
  7. BIN
      practica8_1/bin/practica8_1/Gente.class
  8. BIN
      practica8_1/bin/practica8_1/Main.class
  9. BIN
      practica8_1/bin/practica8_1/ObjReader.class
  10. BIN
      practica8_1/bin/practica8_1/ObjWriter.class
  11. BIN
      practica8_1/bin/practica8_1/Persona.class
  12. +35
    -0
      practica8_1/src/practica8_1/Gente.java
  13. +26
    -0
      practica8_1/src/practica8_1/Main.java
  14. +21
    -0
      practica8_1/src/practica8_1/ObjReader.java
  15. +21
    -0
      practica8_1/src/practica8_1/ObjWriter.java
  16. +38
    -0
      practica8_1/src/practica8_1/Persona.java

+ 10
- 0
practica8_1/.classpath View File

@ -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>

+ 17
- 0
practica8_1/.project View File

@ -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>

+ 2
- 0
practica8_1/.settings/org.eclipse.core.resources.prefs View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

+ 14
- 0
practica8_1/.settings/org.eclipse.jdt.core.prefs View File

@ -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

BIN
practica8_1/Persona.bin View File


+ 3
- 0
practica8_1/README.md View File

@ -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] ```

BIN
practica8_1/bin/practica8_1/Gente.class View File


BIN
practica8_1/bin/practica8_1/Main.class View File


BIN
practica8_1/bin/practica8_1/ObjReader.class View File


BIN
practica8_1/bin/practica8_1/ObjWriter.class View File


BIN
practica8_1/bin/practica8_1/Persona.class View File


+ 35
- 0
practica8_1/src/practica8_1/Gente.java View File

@ -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();
}
}

+ 26
- 0
practica8_1/src/practica8_1/Main.java View File

@ -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());
}
}
}

+ 21
- 0
practica8_1/src/practica8_1/ObjReader.java View File

@ -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;
}
}

+ 21
- 0
practica8_1/src/practica8_1/ObjWriter.java View File

@ -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);
}
}
}

+ 38
- 0
practica8_1/src/practica8_1/Persona.java View File

@ -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+"";
}
}

Loading…
Cancel
Save