diff --git a/Serializable/bin/package1/EscribirFichero.class b/Serializable/bin/package1/EscribirFichero.class index 743199a..5518a4f 100644 Binary files a/Serializable/bin/package1/EscribirFichero.class and b/Serializable/bin/package1/EscribirFichero.class differ diff --git a/Serializable/src/package1/EscribirFichero.java b/Serializable/src/package1/EscribirFichero.java index aa6af9a..7327d1e 100644 --- a/Serializable/src/package1/EscribirFichero.java +++ b/Serializable/src/package1/EscribirFichero.java @@ -2,40 +2,71 @@ package package1; import java.io.*; public class EscribirFichero { - public static void main(String[] args) throws IOException{ + public static void main(String[] args) throws IOException, ClassNotFoundException{ Persona persona; File fichero = new File("C:\\Users\\yaros\\Documents\\project\\serial.dat"); FileOutputStream fileout = new FileOutputStream(fichero); ObjectOutputStream dataOS= new ObjectOutputStream(fileout); String[] nombres= {"Juan","Pedro","Perico","Andrés"}; int[] edades= {21,22,23,24}; + byte[] bytes = null; for (int i = 0; i < edades.length; i++) { persona = new Persona(nombres[i],edades[i]); - dataOS.writeObject(persona); + ByteArrayOutputStream bs= new ByteArrayOutputStream(); + ObjectOutputStream os = new ObjectOutputStream (bs); + os.writeObject(persona); // this es de tipo DatoUdp + os.close(); + bytes = bs.toByteArray(); } + + ByteArrayInputStream bs= new ByteArrayInputStream(bytes); // bytes es el byte[] + ObjectInputStream is = new ObjectInputStream(bs); + Persona unObjetoSerializable = (Persona)is.readObject(); + System.out.println(is); + + for(int i = 0; i < bytes.length; i++) { + System.out.print(bytes[i]); + dataOS.writeObject(bytes[i]); + } + System.out.println(); + //dataOS.writeObject(bytes); dataOS.close(); - leerFichObject(fichero); + System.out.println("Done"); + //leerFichObject(fichero); + lol(); } - 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(); - + public static void lol() throws IOException { + File fichero = new File("C:\\Users\\yaros\\Documents\\project\\serial.dat"); + FileReader fr = new FileReader(fichero); + FileInputStream filein = new FileInputStream(fichero); + ObjectInputStream dataIS = new ObjectInputStream(filein); + + int valor=fr.read(); + while(valor!=-1){ + System.out.print((char)valor); + valor=fr.read(); + } } + 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(); + + } } \ No newline at end of file