|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Jun 2007
Messaggi: 3
|
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 |
|
Senior Member
Iscritto dal: Jul 2002
Messaggi: 4334
|
La devi intercettare:
Codice:
try {
...
} catch(IOException ioe) {
...
}
__________________
|Java Base| |
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Jun 2007
Messaggi: 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!!! PS ottima la guida!!! grazie1000!!! Ultima modifica di guido1000 : 18-06-2007 alle 20:01. |
|
|
|
|
|
#4 |
|
Junior Member
Iscritto dal: Jun 2007
Messaggi: 3
|
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 |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 13:37.











|








