PDA

View Full Version : Errore compilazione FileInputStream - mio prg.java


guido1000
18-06-2007, 18:38
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!!!

lovaz
18-06-2007, 18:44
La devi intercettare:
try {
...
} catch(IOException ioe) {
...
}

Prova a dare una letta alla guida che ho in firma ;)

guido1000
18-06-2007, 18:52
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!!!

guido1000
18-06-2007, 20:14
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