GreysitoErPutoAmo

FicheroObjeto - Main

Mar 10th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. public class Main {
  2.  
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.  
  9.        
  10.         try {
  11.             ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream("objetos.data"));
  12.        
  13.            
  14.             for (int i=0; i<5; i++){
  15.                
  16.                 Persona p = new Persona(i);
  17.                
  18.                 oos.writeObject(p);
  19.                            
  20.             }
  21.            
  22.             oos.close();
  23.            
  24.         } catch (FileNotFoundException e) {
  25.             // TODO Auto-generated catch block
  26.             e.printStackTrace();
  27.         } catch (IOException e) {
  28.             // TODO Auto-generated catch block
  29.             e.printStackTrace();
  30.         }
  31.        
  32.         ObjectInputStream ois;
  33.        
  34.         try {
  35.             ois = new ObjectInputStream (new FileInputStream("objetos.data"));
  36.            
  37.             try {
  38.                 Object aux = ois.readObject();
  39.                
  40.                 while (aux!=null){
  41.                    
  42.                     if (aux instanceof Persona){
  43.                         System.out.println(aux);
  44.                     }
  45.                     ois.readObject();
  46.                 }
  47.             } catch (ClassNotFoundException e) {
  48.                 // TODO Auto-generated catch block
  49.                 e.printStackTrace();
  50.             }
  51.            
  52.         } catch (FileNotFoundException e) {
  53.             // TODO Auto-generated catch block
  54.             e.printStackTrace();
  55.         } catch (IOException e) {
  56.             // TODO Auto-generated catch block
  57.             e.printStackTrace();
  58.         }
  59.     }
  60.  
  61. }
Add Comment
Please, Sign In to add comment