Serializacja danych

0

Witam,
Mam stworzone dwie klasy Author i InfoCard, obie rozszerzają interfejs Serializable. Obiekt klasy Author jest polem klasy InfoCard. Gdy próbuję przeprowadzić serializację obiektu klasy InfoCard otrzymuję wyjątek NotSerializableException : Author ...

Jak sobie z tym poradzić?

Funkcja odpowiedzialna za serializację :

void saveToBinaryFile(String fileName){
	    FileOutputStream fos = null;
        ObjectOutputStream oos = null;
		try{
		    fos = new FileOutputStream(fileName);
			oos = new ObjectOutputStream(fos);
		    for(int i=0;i<check;i++)
               oos.writeObject(tab[i]);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
            try {
               if (oos != null) oos.close();
            } catch (IOException e) {}
            try {
               if (fos != null) fos.close();
            } catch (IOException e) {}
		}
    } 
0

A czy wszystkie pola w obu klasach są serializowane?

0

Chyba tak, chociaż nie wiem jak to sprawdzić. Ostatecznie mam taką strukturę :

klasa Author : dwa obiekty String
klasa InfoCard : obiekt Author,obiekt String, int, double
klasa InfoCardList : tablica obiektow InfoCard, int, int

wszystkie 3 kalsy implementują interfejs serializable, serializację próbuje przeprowadzić dla obiektu klasy InfoCardList teraz w ten sposób :

static void saveToBinaryFile(InfoCardList a, String fileName){
	    FileOutputStream fos = null;
        ObjectOutputStream oos = null;
		try{
		    fos = new FileOutputStream(fileName);
			oos = new ObjectOutputStream(fos);
		    oos.writeObject(a);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
            try {
               if (oos != null) oos.close();
            } catch (IOException e) {}
            try {
               if (fos != null) fos.close();
            } catch (IOException e) {}
		}
    }

Jednak cały czas dostaję ten sam wyjątek dla klasy Author (java.io.NotSerializableException : Author)

0

A pokaż jeszcze kod klasy Autor...

0
import java.io.Serializable;

public class Author implements Serializable {
   
   private String name = null;
   private String surname = null;
   
   Author(){
     name = "Jan";
     surname = "Kowalski";	 
   }
   
   Author(String name, String surname){
      this.name = name;
	  this.surname = surname;
   }
   
   public String returnName(){
      return name;
   }
   
   public String returnSurname(){
      return surname;
   }
   
   public String toString(){
      return (surname+" "+name);
   }
   
   public void setName(String name){
      this.name = name;
   }
   
   public void setSurname(String surname) {
      this.surname = surname;
   }
   
   public boolean equals(Object other){
      if(this == other) return true;
	  if(other == null) return false;
	  if(getClass() != other.getClass()) return false;
	  
	  Author o = (Author) other;
	  return (surname.equals(o.surname) && name.equals(o.name));
   }
}
0

Chociaż serializacje pojedynczego obiektu kalsy Author moge przeprowadzicć

0
  1. Konstruktor musi być public lub protected
  2. Gettery muszą się nazywać getX() lub isX(), a nie returnX()

1 użytkowników online, w tym zalogowanych: 0, gości: 1