|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Dec 2009
Messaggi: 5
|
[JAVA]Problema con ObjectInportStream
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:
Codice:
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");
}
}
I file Item.bin e Brand.bin creati a parte con eclipse vengono trovati senza problemi ma continua a darmi un IOException |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2006
Città: Roma
Messaggi: 1383
|
"creati a parte con eclipse"?
|
|
|
|
|
|
#3 | |
|
Junior Member
Iscritto dal: Dec 2009
Messaggi: 5
|
Quote:
|
|
|
|
|
|
|
#5 |
|
Junior Member
Iscritto dal: Dec 2009
Messaggi: 5
|
Codice:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: Brand |
|
|
|
|
|
#7 |
|
Junior Member
Iscritto dal: Dec 2009
Messaggi: 5
|
Codice:
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;
}
}
|
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
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: Codice:
/*
* 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");
}
}
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
|
#9 |
|
Junior Member
Iscritto dal: Dec 2009
Messaggi: 5
|
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
Grazie mille per l'aiuto!!! A quest'ora sarei ancora convinto che il problema era nel codice.... |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 12:48.



















