Advertisement
gonzalob

Untitled

Jun 5th, 2020
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. package archivos;
  2.  
  3. import java.io.EOFException;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9.  
  10. public class Main {
  11.  
  12.     public static void main(String[] args)
  13.     {
  14.     //  Temperatura.escribir();
  15.     //  Temperatura.leer();
  16.        
  17.         try
  18.         {
  19.             FileOutputStream fileOutputStream = new FileOutputStream("personas.dat");
  20.             ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
  21.            
  22.             objectOutputStream.writeObject(new Persona("gonzalo"));
  23.             objectOutputStream.writeObject(new Persona("gabriel"));
  24.             objectOutputStream.writeObject(new Persona("gustavo"));
  25.            
  26.             objectOutputStream.close();
  27.         }
  28.         catch (IOException e)
  29.         {
  30.             e.printStackTrace();
  31.         }
  32.        
  33.         try
  34.         {
  35.             FileInputStream fileInputStream = new FileInputStream("personas.dat");
  36.             ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
  37.             Persona aux ;
  38.             while ((aux = (Persona)inputStream.readObject())!=null)
  39.             {
  40.                 System.out.println(aux.toString());
  41.             }
  42.             inputStream.close();
  43.         }
  44.         catch (EOFException e)
  45.         {
  46.             // TODO: handle exception
  47.         }
  48.         catch (IOException ex)
  49.         {
  50.            
  51.         }
  52.         catch (ClassNotFoundException ex)
  53.         {
  54.             ex.printStackTrace();
  55.         }
  56.        
  57.  
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement