|
|
@ -0,0 +1,48 @@ |
|
|
|
package antoniofrische.com; |
|
|
|
|
|
|
|
import java.io.EOFException; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.ObjectInputStream; |
|
|
|
import java.io.ObjectOutputStream; |
|
|
|
|
|
|
|
public class MainSeriable { |
|
|
|
public static void main(String[] args) throws IOException{ |
|
|
|
Persona persona; |
|
|
|
File fichero = new File("C:\\Users\\AntonioFrische\\OneDrive - ABACCO Solutions\\Documents\\Schule_22_23_CFGS\\Acceso_Datos\\SpringTool_4\\Persona.dat"); |
|
|
|
FileOutputStream fileout = new FileOutputStream(fichero); |
|
|
|
ObjectOutputStream dataOS= new ObjectOutputStream(fileout); |
|
|
|
String[] nombres= {"Juan","Pedro","Perico","Andrés"}; |
|
|
|
int[] edades= {21,22,23,24}; |
|
|
|
for (int i = 0; i < edades.length; i++) { |
|
|
|
persona = new Persona(nombres[i],edades[i]); |
|
|
|
dataOS.writeObject(persona); |
|
|
|
} |
|
|
|
dataOS.close(); |
|
|
|
leerFichObject(fichero); |
|
|
|
} |
|
|
|
|
|
|
|
public static void leerFichObject (File fichero) |
|
|
|
throws IOException { |
|
|
|
Persona persona; |
|
|
|
FileInputStream filein = new FileInputStream(fichero); |
|
|
|
ObjectInputStream dataIS = new ObjectInputStream(filein); |
|
|
|
try { |
|
|
|
while (true) { |
|
|
|
persona = (Persona) dataIS.readObject(); |
|
|
|
System.out.println("Nombre: "+persona.getNombre()+ |
|
|
|
" Edad: "+persona.getEdad()); |
|
|
|
} |
|
|
|
} catch (EOFException error) { |
|
|
|
//nada |
|
|
|
} catch (ClassNotFoundException error) { |
|
|
|
error.printStackTrace(); |
|
|
|
System.out.println(error.getMessage()); |
|
|
|
} |
|
|
|
dataIS.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |