Browse Source

commit

master
yaroslav 1 year ago
parent
commit
4a4b773529
2 changed files with 53 additions and 22 deletions
  1. BIN
      Serializable/bin/package1/EscribirFichero.class
  2. +53
    -22
      Serializable/src/package1/EscribirFichero.java

BIN
Serializable/bin/package1/EscribirFichero.class View File


+ 53
- 22
Serializable/src/package1/EscribirFichero.java View File

@ -2,40 +2,71 @@ package package1;
import java.io.*; import java.io.*;
public class EscribirFichero { public class EscribirFichero {
public static void main(String[] args) throws IOException{
public static void main(String[] args) throws IOException, ClassNotFoundException{
Persona persona; Persona persona;
File fichero = new File("C:\\Users\\yaros\\Documents\\project\\serial.dat"); File fichero = new File("C:\\Users\\yaros\\Documents\\project\\serial.dat");
FileOutputStream fileout = new FileOutputStream(fichero); FileOutputStream fileout = new FileOutputStream(fichero);
ObjectOutputStream dataOS= new ObjectOutputStream(fileout); ObjectOutputStream dataOS= new ObjectOutputStream(fileout);
String[] nombres= {"Juan","Pedro","Perico","Andrés"}; String[] nombres= {"Juan","Pedro","Perico","Andrés"};
int[] edades= {21,22,23,24}; int[] edades= {21,22,23,24};
byte[] bytes = null;
for (int i = 0; i < edades.length; i++) { for (int i = 0; i < edades.length; i++) {
persona = new Persona(nombres[i],edades[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(); 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();
}
} }

Loading…
Cancel
Save