Advertisement
nRikee

pract4 carregar(), guardar()

May 8th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1.     /**
  2.      * Afegeix al dibuix un dibuix guardat en un fitxer.
  3.      * @param nomFitxer nom del fitxer.
  4.      */
  5.     public void carregar(String nomFitxer) throws Exception {  
  6.          /* Definició dels fluxes d'entrada i eixida (FileInputStream i ObjectInputStream) */          
  7.          FileInputStream fentrada = null;
  8.          ObjectInputStream fitx = null;
  9.          /* Iterar fins trobar un fitxer nomFitxer. En cas de no trobar-lo, sol.licitar un altre nom
  10.           * des de teclat. Si el nom es el String buit, acabar el metode sense carregar cap dibuix.
  11.           * Per tal d'averiguar si un fitxer existeix es pot usar l'excepció FileNotFoundException.
  12.           *
  13.           * Utilitzar les classes FileInputStream(String) i ObjectInputStream(FileInputStream) per
  14.           * crear el fluxe al fitxer. Aquestes dues instruccions han d'estar en una instrucció try-catch
  15.           * que capture l'excepcio FileNotFoundException. En cas que es produisca, mostrar un missatge
  16.           * per pantalla i sol.licitar un altre nom de fitxer des de teclat. La resta de tipus d'excepcions
  17.           * es propaguen.
  18.           *
  19.           * La iteració acaba si el fitxer no s'ha trobat (fitx==null) i el nom del fitxer es la
  20.           * cadena buida o si el fitxer s'ha trobat (fitx!=null) i el nom del fitxer no es la cadena buida.
  21.           *
  22.           * Despres del bucle:
  23.           * Si s'ha trobat el fitxer (fitx!=null), llegir-lo usant la instruccio readObject() i
  24.           * convertir l'objecte llegit a tipus Dibuix usant un casting e inserir-lo al dibuix
  25.           * this invocant al metode privat inserirDibuix(Dibuix).      
  26.           *
  27.           * Tancar el fluxe fitx.
  28.           * Podeu usar el seguent esquema:
  29.           *
  30.           *     do {
  31.           *         try {
  32.           *             fentrada = ...
  33.           *             fitx = ...
  34.           *         }catch(   ...  ){
  35.           *              ...
  36.           *         };
  37.           *     }while( ... );
  38.           *     if (fitx!=null) { ... }
  39.           */
  40.          boolean existeix = false;
  41.          FileOutputStream fo = null;
  42.          
  43.          //ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fitxer));
  44.          
  45.          do{
  46.              try{
  47.                  fo = new FileOutputStream(this.nom);
  48.                  existeix = true;
  49.              }
  50.              catch ( FileNotFoundException e){
  51.                 System.out.print("El fitxer no s'ha trobat. Escriu-ne uno de nou: ");
  52.                 nomFitxer = teclat.next();
  53.                 existeix = false;
  54.              }
  55.          } while ( fitx != null && nomFitxer != "" );
  56.            
  57.          if( existeix ){
  58.              FileInputStream fi = new FileInputStream(nomFitxer);
  59.              ObjectInputStream fio = new ObjectInputStream(fi);
  60.              Dibuix nd = (Dibuix) fio.readObject();
  61.              
  62.              if( nd != null )
  63.                  inserirDibuix(nd);
  64.          }
  65.          // IMPLEMENTAR
  66.          
  67.     }
  68.      
  69.     /**
  70.      * Guarda un dibuix en un fitxer.
  71.      * El nom del fitxer es l'atribut nom del dibuix.
  72.      */
  73.     public void guardar() throws Exception {
  74.         /* Definir un objecte de tipus File per aplicar-li el metode exists() i
  75.          * averiguar si ja existeix un fitxer amb el nom del dibuix.
  76.          * En cas que existica, preguntar a l'usuari si vol sobreescriure'l.
  77.          *      En cas negatiu, no fer res.
  78.          *      En cas positiu:
  79.          *          Crear el fluxe d'eixida:
  80.          *              FileOutputStream fentrada = new FileOutputStream(nom);
  81.          *              ObjectOutputStream fitx = new ObjectOutputStream(fentrada);
  82.          *      Escriure el dibuix.
  83.          *      Canviar l'estat de la variable guardat a false.
  84.          *      Tancar el fitxer.
  85.          */
  86.         File f = new File(nom);
  87.         char sobreescriure = 'x';
  88.         if ( f.exists() ){
  89.             do{
  90.                 System.out.print("Vols sobreescriure-lo? (S/N)");
  91.                 sobreescriure = teclat.next().toUpperCase().charAt(0);
  92.             } while (sobreescriure != 'S' && sobreescriure != 'N' );
  93.            
  94.             if ( sobreescriure == 'S' ){
  95.                 FileOutputStream fentrada = new FileOutputStream(nom);
  96.                 ObjectOutputStream fitx = new ObjectOutputStream(fentrada);
  97.                 fitx.writeObject(this);
  98.                 guardat = false;
  99.                 fitx.close();
  100.             }
  101.         }
  102.         // IMPLEMENTAR
  103.        
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement