Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Errore compilazione FileInputStream - mio prg.java

    Ciao a tutti gli utenti del forum!!!
    Ho un piccolo problema con un mio programmino java

    ecco il codice (scritto da me) che mi da problemi:

    //Carica un database serializzato da un file binario.
    public void carica(String nomeFile) {
    FileInputStream fis = new FileInputStream(nomeFile);
    ObjectInputStream ois = new ObjectInputStream(fis);
    DatabaseAuto da = (DatabaseAuto)ois.readObject(); //DatabaseAuto è la classe in cui si trova questo metodo
    ois.close();
    fis.close();
    }

    Il main richiama questo metodo in questo modo:
    dbAuto1.carica("databaseAuto.bin");

    --------------------------------------
    ERRORI:

    test.java:153: unreported exception java.io.FileNotFoundException; must be caugh
    t or declared to be thrown
    FileInputStream fis = new FileInputStream(nomeFile);
    ^
    test.java:154: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    ObjectInputStream ois = new ObjectInputStream(fis);
    ^
    test.java:155: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    DatabaseAuto da = (DatabaseAuto)ois.readObject();
    ^
    test.java:156: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    ois.close();
    ^
    test.java:157: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    fis.close();
    ^
    test.java:255: unreported exception java.io.FileNotFoundException; must be caugh
    t or declared to be thrown
    FileOutputStream fos = new FileOutputStream(nomeFile);
    ^
    test.java:256: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    ^
    test.java:257: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    oos.writeObject(sA);
    ^
    test.java:258: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    oos.writeObject(sT);
    ^
    test.java:259: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    oos.flush();
    ^
    test.java:260: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    oos.close();
    ^
    test.java:261: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    fos.close();
    ^
    12 errors

    ---------------------------------

    io ho pensato di dichiarare il metodo:
    public void carica(String nomeFile) throws IOException, ClassNotFoundException {...}
    ma così facendo mi da errore nella chiamata del metodo nel blocco main.
    E non posso modificare il main.
    Ovviamente ho importato java.io.*; (import java.io.*
    FileInputStream lo potete trovare all'indirizzo http://java.sun.com/j2se/1.5.0/docs/api/
    ObjectInputStream lo potete trovare all'indirizzo http://java.sun.com/j2se/1.5.0/docs/api/

    Spero che qualcuno mi posso aiutare!!! in anticipo: GRAZIE!!!

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    Alcuni tipi di eccezioni in java vanno obbligatoriamente gestite.
    Consiglio di leggere una guida java, nella parte che tratta il controllo delle eccezioni.
    Prova alcune di queste PAGINE

  3. #3
    Ho provato a fare

    //Carica un database serializzato da un file binario.
    public void carica(String nomeFile) throws IOException {
    try {
    FileInputStream fis = new FileInputStream(nomeFile);
    ObjectInputStream ois = new ObjectInputStream(fis);
    DatabaseAuto da = (DatabaseAuto)ois.readObject();
    ois.close();
    fis.close();
    } catch (IOException e) {
    throw new IOException("errore");
    }
    }

    ma mi da questo errore:

    test.java:110: unreported exception java.io.IOException; must be caught or decla
    red to be thrown
    dbAuto1.carica("databaseAuto.bin");
    ^
    1 error

    e io non posso modificare questa riga di codice. il main non l'ho fatto io e non posso modificarlo. Quindi c'è sicuramente un altro modo x farlo funzionare...
    ??? sapresti darmi un altro consiglio??? grazie!!!

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    codice:
    public void carica(String nomeFile) throws IOException {
    try {
    FileInputStream fis = new FileInputStream(nomeFile);
    ObjectInputStream ois = new ObjectInputStream(fis);
    DatabaseAuto da = (DatabaseAuto)ois.readObject();
    ois.close();
    fis.close();
    } catch (IOException e) {
    throw new IOException("errore");
    }
    }
    Se dici al main che questo metodo puo' lanciare l'eccezione, e' ovvio che il main s'aspetti di doverla gestire. Se gestisci l'eccezione dentro carica, devi togliere la clausola throws.
    Altri metodi non ci sono. Il meccanismo delle eccezioni in java e' molto rigido, per fortuna...

  5. #5
    ok grazie!!! ho sistemato il tutto in questo modo:

    //Carica un database serializzato da un file binario.
    public void carica(String nomeFile) {
    try {
    FileInputStream fis = new FileInputStream(nomeFile);
    ObjectInputStream ois = new ObjectInputStream(fis);
    DatabaseAuto da = (DatabaseAuto)ois.readObject();
    ois.close();
    fis.close();
    } catch (IOException e) {
    System.out.println("errore tipo IO");
    } catch (ClassNotFoundException e2) {
    System.out.println("errore tipo ClassNotFound");
    }
    }



    GRAZIE DI TUTTO

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.