Arcano01
01-04-2006, 13:32
Salve, come da titolo ho un problema nella lettua di file strutturati.
La lettura avviene correttamente solo se nel file strutturato è stata effettuata una sola scrittura.Se invece, con modalità append, vengono aggiunti nuovi dati al file strutturato, la lettura (che avviene fino a generazione di eccezione per raggiungimento della fine del file) comporta la lettura delle informazioni inserite nella prima mscrittura, e nulla di più.
pwer la scrittura il codice è il seguente
FileOutputStream f = new FileOutputStream ("myfile.dat",true);
ObjectOutputStream fOut = new ObjectOutputStream (f);
fOut.writeObject(myObject);
fOut.flush();
f.close();
per la lettura
myClass newObject;
try{
FileInputStream f = new FileInputStream ("myfile.dat");
ObjectInputStream fIN = new ObjectInputStream (giocate);
while(true)
try{
newObject = (myClass) giocateIN.readObject();
System.out.println(newObject.num1 +" "+
newObject.num2);
}
catch(EOFException e){
//interrompe il ciclo
break;
}
f.close();
}
catch(Exception e){
System.out.println("ERROR"+e.getCause());
}
FileOutputStream f = new FileOutputStream ("myfile.dat",true);
ObjectOutputStream fOut = new ObjectOutputStream (f);
fOut.writeObject(myObject);
fOut.flush();
f.close();
ES.
2 3
3 6
7 8
Quando leggo nel file ottengo
2 3
ERRORNull
Graziw
La lettura avviene correttamente solo se nel file strutturato è stata effettuata una sola scrittura.Se invece, con modalità append, vengono aggiunti nuovi dati al file strutturato, la lettura (che avviene fino a generazione di eccezione per raggiungimento della fine del file) comporta la lettura delle informazioni inserite nella prima mscrittura, e nulla di più.
pwer la scrittura il codice è il seguente
FileOutputStream f = new FileOutputStream ("myfile.dat",true);
ObjectOutputStream fOut = new ObjectOutputStream (f);
fOut.writeObject(myObject);
fOut.flush();
f.close();
per la lettura
myClass newObject;
try{
FileInputStream f = new FileInputStream ("myfile.dat");
ObjectInputStream fIN = new ObjectInputStream (giocate);
while(true)
try{
newObject = (myClass) giocateIN.readObject();
System.out.println(newObject.num1 +" "+
newObject.num2);
}
catch(EOFException e){
//interrompe il ciclo
break;
}
f.close();
}
catch(Exception e){
System.out.println("ERROR"+e.getCause());
}
FileOutputStream f = new FileOutputStream ("myfile.dat",true);
ObjectOutputStream fOut = new ObjectOutputStream (f);
fOut.writeObject(myObject);
fOut.flush();
f.close();
ES.
2 3
3 6
7 8
Quando leggo nel file ottengo
2 3
ERRORNull
Graziw