Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Afegeix al dibuix un dibuix guardat en un fitxer.
- * @param nomFitxer nom del fitxer.
- */
- public void carregar(String nomFitxer) throws Exception {
- /* Definició dels fluxes d'entrada i eixida (FileInputStream i ObjectInputStream) */
- FileInputStream fentrada = null;
- ObjectInputStream fitx = null;
- /* Iterar fins trobar un fitxer nomFitxer. En cas de no trobar-lo, sol.licitar un altre nom
- * des de teclat. Si el nom es el String buit, acabar el metode sense carregar cap dibuix.
- * Per tal d'averiguar si un fitxer existeix es pot usar l'excepció FileNotFoundException.
- *
- * Utilitzar les classes FileInputStream(String) i ObjectInputStream(FileInputStream) per
- * crear el fluxe al fitxer. Aquestes dues instruccions han d'estar en una instrucció try-catch
- * que capture l'excepcio FileNotFoundException. En cas que es produisca, mostrar un missatge
- * per pantalla i sol.licitar un altre nom de fitxer des de teclat. La resta de tipus d'excepcions
- * es propaguen.
- *
- * La iteració acaba si el fitxer no s'ha trobat (fitx==null) i el nom del fitxer es la
- * cadena buida o si el fitxer s'ha trobat (fitx!=null) i el nom del fitxer no es la cadena buida.
- *
- * Despres del bucle:
- * Si s'ha trobat el fitxer (fitx!=null), llegir-lo usant la instruccio readObject() i
- * convertir l'objecte llegit a tipus Dibuix usant un casting e inserir-lo al dibuix
- * this invocant al metode privat inserirDibuix(Dibuix).
- *
- * Tancar el fluxe fitx.
- * Podeu usar el seguent esquema:
- *
- * do {
- * try {
- * fentrada = ...
- * fitx = ...
- * }catch( ... ){
- * ...
- * };
- * }while( ... );
- * if (fitx!=null) { ... }
- */
- boolean existeix = false;
- FileOutputStream fo = null;
- //ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fitxer));
- do{
- try{
- fo = new FileOutputStream(this.nom);
- existeix = true;
- }
- catch ( FileNotFoundException e){
- System.out.print("El fitxer no s'ha trobat. Escriu-ne uno de nou: ");
- nomFitxer = teclat.next();
- existeix = false;
- }
- } while ( fitx != null && nomFitxer != "" );
- if( existeix ){
- FileInputStream fi = new FileInputStream(nomFitxer);
- ObjectInputStream fio = new ObjectInputStream(fi);
- Dibuix nd = (Dibuix) fio.readObject();
- if( nd != null )
- inserirDibuix(nd);
- }
- // IMPLEMENTAR
- }
- /**
- * Guarda un dibuix en un fitxer.
- * El nom del fitxer es l'atribut nom del dibuix.
- */
- public void guardar() throws Exception {
- /* Definir un objecte de tipus File per aplicar-li el metode exists() i
- * averiguar si ja existeix un fitxer amb el nom del dibuix.
- * En cas que existica, preguntar a l'usuari si vol sobreescriure'l.
- * En cas negatiu, no fer res.
- * En cas positiu:
- * Crear el fluxe d'eixida:
- * FileOutputStream fentrada = new FileOutputStream(nom);
- * ObjectOutputStream fitx = new ObjectOutputStream(fentrada);
- * Escriure el dibuix.
- * Canviar l'estat de la variable guardat a false.
- * Tancar el fitxer.
- */
- File f = new File(nom);
- char sobreescriure = 'x';
- if ( f.exists() ){
- do{
- System.out.print("Vols sobreescriure-lo? (S/N)");
- sobreescriure = teclat.next().toUpperCase().charAt(0);
- } while (sobreescriure != 'S' && sobreescriure != 'N' );
- if ( sobreescriure == 'S' ){
- FileOutputStream fentrada = new FileOutputStream(nom);
- ObjectOutputStream fitx = new ObjectOutputStream(fentrada);
- fitx.writeObject(this);
- guardat = false;
- fitx.close();
- }
- }
- // IMPLEMENTAR
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement