Advertisement
Guest User

asdf

a guest
Oct 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.18 KB | None | 0 0
  1.        
  2.         List<Curso> cursos=new ArrayList();
  3.        
  4.             Scanner sc=new Scanner(System.in);
  5.             System.out.println("¿Qué deseas hacer? \n 1)Añadir 2)Modificar 3)Borrar 4)Listar" );
  6.             int opcion=sc.nextInt();
  7.             sc.nextLine();
  8.        
  9.         FileOutputStream fileout=null;
  10.         ObjectOutputStream dataOS=null;
  11.         FileOutputStream fileout2=null;
  12.         ObjectOutputStream dataOS2=null;
  13.         FileInputStream filein=null;
  14.         ObjectInputStream dataIS=null;
  15.        
  16.        
  17.         Curso c=null;
  18.         try {
  19.  
  20.             File fichero = new File("C:\\Users\\usuario\\Desktop\\NuevaCarpeta\\cursos2.dat");
  21.  
  22.  
  23.             switch (opcion){
  24.                 case 1:
  25.  
  26.                     System.out.println("Introduce nombre, id, id de departamento, precio, dificultad");
  27.                     String nombre=sc.nextLine();
  28.                     int id=sc.nextInt();
  29.                     sc.nextLine();
  30.                     String idDep=sc.nextLine();
  31.                     float precio=sc.nextFloat();
  32.                     sc.nextLine();
  33.                     String dificultad=sc.nextLine();
  34.                    
  35.                     if (fichero.exists()){
  36.                         filein=new FileInputStream(fichero);
  37.                         dataIS=new ObjectInputStream(filein);
  38.                     try {
  39.                        
  40.                         while (true){
  41.                          cursos.add((Curso)dataIS.readObject());
  42.                          }
  43.                         } catch (EOFException eo) {}
  44.                         dataIS.close(); //cerrar stream de entrada
  45.                     }
  46.                    
  47.                         cursos.add(new Curso(nombre, id, dificultad, precio, idDep));
  48.                        
  49.                          fileout = new FileOutputStream(fichero);
  50.                          dataOS = new ObjectOutputStream(fileout);
  51.                      for (int i = 0; i < cursos.size(); i++) {
  52.                          dataOS.writeObject(cursos.get(i));
  53.                     }
  54.  
  55.                    
  56.                    
  57.                    
  58.                     dataOS.close();
  59.                     fileout.close();
  60.  
  61.                      break;
  62.                 case 2:
  63.                     System.out.println("Id del curso que deseas modificar: ");
  64.                     int idModificar=sc.nextInt();
  65.                     sc.nextLine();
  66.                     System.out.println("Introduce nombre, id, id de departamento, precio, dificultad");
  67.                     String nombreM=sc.nextLine();
  68.                     int idM=sc.nextInt();
  69.                     sc.nextLine();
  70.                     String idDepM=sc.nextLine();
  71.                     float precioM=sc.nextFloat();
  72.                     sc.nextLine();
  73.                     String dificultadM=sc.nextLine();
  74.  
  75.                    
  76.                 try {
  77.                     filein = new FileInputStream(fichero);
  78.                     dataIS=new ObjectInputStream(filein);
  79.                    
  80.                      while(true){
  81.                         cursos.add((Curso)dataIS.readObject());
  82.                    }  
  83.                 } catch (ClassNotFoundException ex) {
  84.                     Logger.getLogger(EJ_9_AccesoADatos.class.getName()).log(Level.SEVERE, null, ex);
  85.                 }  catch (EOFException eo) {}
  86.                                    
  87.                     File aux=new File("C:\\Users\\Eduardo\\Desktop\\Pruebas\\nuevo_aux");
  88.                     fileout2=new FileOutputStream(aux);
  89.                     dataOS2=new ObjectOutputStream(fileout2);
  90.                
  91.                 for (int i = 0; i < cursos.size(); i++) {
  92.                         if (cursos.get(i).getId()==idModificar){
  93.                         Curso nuevo= new Curso(nombreM, idM, dificultadM, precioM, idDepM);
  94.                         dataOS2.writeObject(nuevo);
  95.                     }else {
  96.                         dataOS2.writeObject(cursos.get(i));
  97.                     }
  98.                     }
  99.                
  100.                
  101.  
  102.        
  103.                 dataIS.close();
  104.                 dataOS2.close();
  105.                
  106.                
  107.                    
  108.                 break;
  109.                
  110.                 case 4:
  111.                     filein = new FileInputStream(fichero);
  112.                     dataIS=new ObjectInputStream(filein);
  113.                     Curso c1=null;
  114.                    
  115.                     try{
  116.                         while(true){
  117.                              c1=(Curso)dataIS.readObject();
  118.                              System.out.println(c1.getNombre()+ " " + c1.getId()+ " "+ c1.getDificultad() + " " + c1.getPrecio()+ " " +c1.getIdDepartamento());
  119.                      }
  120.                     }catch (ClassNotFoundException ex) {
  121.                     Logger.getLogger(EJ_9_AccesoADatos.class.getName()).log(Level.SEVERE, null, ex);
  122.                 }  catch (EOFException eo) {}
  123.                      
  124.                 dataIS.close();
  125.  
  126.                 break;
  127.                    
  128.                    
  129.             }
  130.         } catch (IOException ex) {
  131.             Logger.getLogger(EJ_9_AccesoADatos.class.getName()).log(Level.SEVERE, null, ex);
  132.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement