-1

Can someone help me to serialze an object into a string? The result of my code is a bit weird, I need to get a toString(); methode or something with which i can serialze an object into a string but I dont know any.

Thanks for the help

results with getString -> only "null"

result without getString(); -> Fruit@4dd8dc3, Fruit@6d03e736, Fruit@568db2f2, Fruit@378bf509, Fruit@5fd0d5ae, Fruit@2d98a335, Fruit@16b98e56, Fruit@7ef20235"

import java.io.Serializable;

public class Fruit implements Comparable<Fruit>,Serializable{

    String getString;
    String name;
    int gewicht;

    public String getString() {

            return this.getString;

          }

    public Fruit(String name, int gewicht) {
        this.name=name;
        this.gewicht=gewicht;
    }

    public int compareTo(Fruit otherFruit) {

        if(this.gewicht < otherFruit.gewicht)
            return -1;
        if(this.gewicht>otherFruit.gewicht)
            return 1;
        return 0;
    }


}




import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Iterator;
import java.util.TreeSet;

public class FruitTree {

    public static void main(String[] args) {

        TreeSet<Fruit> fruitTree = new TreeSet<Fruit>();

        fruitTree.add(new Fruit("Kiwi",5));
        fruitTree.add(new Fruit("Kirsche",1));
        fruitTree.add(new Fruit("Ananas",75));
        fruitTree.add(new Fruit("Zitrone",15));
        fruitTree.add(new Fruit("Grapefruit",44));
        fruitTree.add(new Fruit("Banane",55));
        fruitTree.add(new Fruit("Kirsche",2));
        fruitTree.add(new Fruit("Kiwi",8));

        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("IO.txt"));
            Iterator<Fruit> it = fruitTree.iterator();



            while(it.hasNext()) {
            oos.writeObject(it.next());
        }
            oos.writeObject(null);



            ObjectInputStream ois = new ObjectInputStream (new FileInputStream("IO.txt"));

            Object readObject=null;
            TreeSet<Fruit> deserializedFruits= new TreeSet<Fruit>();

            do {
                readObject=ois.readObject();
                if(readObject !=null)
                    deserializedFruits.add((Fruit) readObject);
            }
            while (readObject!=null);

            it=deserializedFruits.iterator();
            while(it.hasNext()) {
                System.out.println(it.next().getString());
                ois.close();
                oos.close();
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }


        }




    }

Can someone help me to serialze an object into a string? The result of my code is a bit weird, I need to get a toString(); methode or something with which i can serialze an object into a string but I dont know any.

Thanks for the help

1 Answers1

0

Adding implements Serializable to your class will not override default Object.toString implementation - that's not how Java works (btw this default implementation will actually provide you what you got - for example Fruit@5fd0d5ae - that's how it works)

what you need to do is to override toString by yourself - for example

public class Fruit implements Comparable<Fruit>,Serializable {
    // ...

    @Override
    public String toString() {
        return "name: " + this.name + ", gewicht: " + String.valueOf(this.gewicht);
    }
}

or to use some existing tool that will generate this method for you (like lombok), or even better that will allow you to serialize your class object to some common format like JSON or XML (for this take a look at gson or Jackson)

m.antkowicz
  • 13,268
  • 18
  • 37