Advertisement
gonzalob

Untitled

Jun 21st, 2022
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package archivos2;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.EOFException;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.NotSerializableException;
  12. import java.io.ObjectInputStream;
  13. import java.io.ObjectOutputStream;
  14. import java.util.ArrayList;
  15.  
  16. public class Main
  17. {
  18.     private static File f = new File("datos.bin");
  19.  
  20.    
  21.     public static void main(String[] args) {
  22.        
  23.         try
  24.         {
  25.             if (!f.exists())
  26.             {
  27.                 System.out.println("No existe");
  28.                 escribirNuevo();
  29.             }
  30.             else
  31.             {
  32.                 System.out.println("Existe");
  33.                 escribirAgregar();
  34.             }
  35.             leer();
  36.         }
  37.         catch (EOFException e) {
  38.            
  39.             System.out.println("FIN del Archivo");
  40.         }
  41.         catch (FileNotFoundException e) {
  42.             // TODO Auto-generated catch block
  43.             e.printStackTrace();
  44.         }
  45.         catch (NotSerializableException e) {
  46.                 e.printStackTrace();
  47.         }
  48.         catch (IOException e) {
  49.             // TODO Auto-generated catch block
  50.             e.printStackTrace();
  51.         } catch (ClassNotFoundException e) {
  52.             // TODO Auto-generated catch block
  53.             e.printStackTrace();
  54.         } catch (Exception e) {
  55.             // TODO Auto-generated catch block
  56.             e.printStackTrace();
  57.         }
  58.        
  59.     }
  60.    
  61.     public static void leer() throws IOException, ClassNotFoundException
  62.     {
  63.         if (f.exists())
  64.         {      
  65.             FileInputStream fileInputStream = new FileInputStream(f);
  66.            
  67.             ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
  68.            
  69.             int lectura = 1;
  70.            
  71.             while (lectura == 1 )
  72.             {
  73.                 Persona aux = (Persona)inputStream.readObject();
  74.                 System.out.println(aux.getNombre());
  75.             }
  76.            
  77.             inputStream.close();
  78.         }
  79.     }
  80.    
  81.     public static void escribirNuevo() throws Exception
  82.     {
  83.         FileOutputStream fileOutputStream = new FileOutputStream(f);
  84.        
  85.         ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
  86.        
  87.         objectOutputStream.writeObject(getPersona("gonzalo"));
  88.        
  89.         fileOutputStream.close();
  90.        
  91.         objectOutputStream.close();
  92.     }
  93.    
  94.     public static void escribirAgregar() throws Exception
  95.     {
  96.         FileOutputStream fileOutputStream = new FileOutputStream("datos.bin",true);
  97.        
  98.         MyObjectOutputStream myObjectOutputStream = new MyObjectOutputStream(fileOutputStream);
  99.        
  100.         myObjectOutputStream.writeObject(getPersona("juan"));
  101.        
  102.         fileOutputStream.close();
  103.        
  104.         myObjectOutputStream.close();
  105.     }
  106.    
  107.     public static Persona getPersona(String nombre)
  108.     {
  109.         Persona p = new Persona();
  110.         p.setNombre(nombre);
  111.         return p;
  112.        
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement