PDA

View Full Version : [JAVA]Problema con ObjectInportStream


Vincent_87
10-12-2009, 20:29
Salve a tutti, mi sto da poco cimentando in Java ed è tutto il pomeriggio che sto sbattendo la testa su un problema: non mi riesce di importare oggetti da file. Il codice è il seguente:


public static TreeSetex<Item> item = new TreeSetex<Item>();
public static TreeSetex<Brand> brand = new TreeSetex<Brand>();

public static void main(String[] args) {

FileInputStream initem=null;
FileInputStream inbrand=null;

try {
initem= new FileInputStream("Item.bin");

inbrand= new FileInputStream("Brand.bin");

}catch(FileNotFoundException fnfe){
System.out.println("File non trovato");
System.exit(1);
}
ObjectInputStream sitem=null;
ObjectInputStream sbrand=null;

try{

sitem= new ObjectInputStream(initem);
sbrand= new ObjectInputStream (inbrand);

item=(TreeSetex<Item>) sitem.readObject();
brand=(TreeSetex<Brand>)sbrand.readObject();
sitem.close();
sbrand.close();
}catch(IOException ioe){
System.out.println("Errore in lettura dei file");
System.exit(1);
}catch(ClassNotFoundException e){
System.out.println("ClassNotFoundException");
}

}


TreeSetex è semplicemente TreeSet che implementa Serializable.
I file Item.bin e Brand.bin creati a parte con eclipse vengono trovati senza problemi ma continua a darmi un IOException :confused:

fero86
11-12-2009, 00:31
"creati a parte con eclipse"? :mbe:

Vincent_87
11-12-2009, 08:34
"creati a parte con eclipse"?

Si, nuovo->File..... comunque dovro modificare anche quello visto che cosi a lungo andare quando leggo il file mi troverei un sacco di elementi duplicati.... Per ora però vorrei risolvere questo problema dell'IOException, non riesco proprio a trovare una soluzione:muro:

PGI-Bis
11-12-2009, 09:37
Incolla la traccia completa dell'eccezione rilasciata.

Vincent_87
11-12-2009, 10:36
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: Brand

Mi riporta queste due righe ma la classe Brand (come anche Item) che ho definito implementa Serializable

PGI-Bis
11-12-2009, 11:14
Incolla la classe Brand.

Vincent_87
11-12-2009, 11:50
public class Brand implements Comparable<Object>, Serializable{

String name;
String reference;
String contact;
static JTextField namein= new JTextField("");
static JTextField refin=new JTextField();
static JTextField contactin=new JTextField();

public String getName(){
return name;
}

public static void reset(){
namein.setText("");
contactin.setText("");
refin.setText("");
}

public boolean equals(Object arg0){
Brand b= (Brand) arg0;
return name.equals(b.getName());
}

public int compareTo(Object arg0){
Brand b= (Brand) arg0;
return name.compareTo(b.getName());
}

public static JPanel databrand(JPanel datain, String string){
datain.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), string));
datain.setLayout(new GridLayout(3,2));
JLabel name= new JLabel("Name");
JLabel references=new JLabel("References");
JLabel contact= new JLabel("Contact");
datain.add(name);
datain.add(namein);
datain.add(references);
datain.add(refin);
datain.add(contact);
datain.add(contactin);
return datain;
}
}

PGI-Bis
11-12-2009, 14:00
La classe è ok, la lettura pure, il problema è nei dati serializzati.

Esattamente come serializzi gli oggetti Brand nel file "Brand.bin"? Lo chiedo perchè provando con:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
*
* @author pgi
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Throwable {
FileOutputStream fout = new FileOutputStream("z:\\test.ser");
Brand brand = new Brand();
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(brand);
out.flush();
out.close();
System.out.println("Written");

FileInputStream fin = new FileInputStream("z:\\test.ser");
ObjectInputStream in = new ObjectInputStream(fin);
brand = (Brand) in.readObject();
in.close();
System.out.println("Read");
}

}

Non risultano errori di sorta.

Vincent_87
11-12-2009, 14:29
Incredibilmente adesso funziona, ho lanciato il debugging di eclipse mentre in precedenza mi bastava lanciare l'esecuzione e adesso va. Sinceramente non ho capito bene il motivo ma è come se prima del debugging non mi avesse preso l'implementazione Serializable di Brand, cosa alquanto strana visto che l'ho fatta insieme ad Item e li non mi da problemi..... mah:confused:

Grazie mille per l'aiuto!!! A quest'ora sarei ancora convinto che il problema era nel codice....