Problem mit dem serialisieren von klassen



  • Guten Abend Kollegen der Algorithmen, ich habe ein kleines Problem in Java, im genaueren beim serialisieren von klassen.

    ich habe folgende klasse geschrieben:

    public class SaveAndLoad<T extends Serializable>
    {
        private String dateiFilename;
        private File path;
        public T data = null;
    
        public SaveAndLoad(String filename, T aNew)
        {
            data = aNew;
            path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
            dateiFilename = filename; // TODO: check this
        }
    
        public void load()
        {
            File f = new File(path, dateiFilename);
            if (f.exists() && !f.isDirectory()) {
                try {
                    FileInputStream fileIn = new FileInputStream(f);
    
                    ObjectInputStream in = new ObjectInputStream(fileIn);
                    Class<?> A = data.getClass();
                    data = convertInstanceOfObject(in.readObject(), (Class<T>) A); // TODO: erste Exception hier
                    in.close();
                    fileIn.close();
                }
                catch (IOException i) {
                    i.printStackTrace();
                }
                catch (ClassNotFoundException c) {
                    System.out.println("'T' class not found");
                    c.printStackTrace();
                }
            }
        }
    
        public void save()
        {
            try {
                FileOutputStream fileOut = new FileOutputStream(new File(path, dateiFilename));
                ObjectOutputStream out = new ObjectOutputStream(fileOut);
                out.writeObject(data);
                out.flush();
                out.close();
                fileOut.close();
            }
            catch (IOException i) {
                i.printStackTrace();
            }
        }
    
        public static <T> T convertInstanceOfObject(Object o, Class<T> clazz)
        {
            try {
                return (T) o;
    //            return clazz.cast(o);
            }
            catch (ClassCastException e) {
                return null;
            }
        }
    }
    

    Diese Klasse ist da um eine unbekannte Struktur zu lesen.

    In meinem Fall ist ein Getter und Setter der aus 2 variablen dieser klasse bestehen:

    public class TimeCalculator implements Serializable
    {
        private static final long serialVersionUID = 465551445;
        private long begin, ende;
        private long zeitAbstand = 0;
        private boolean isLive = false, noLimit = false;
        private int status;
        private long lastTime = 0;
    
        public TimeCalculator()
        {
            status = enumtoint(TimerStatus.NOT_STARTED);
        }
    
        public void setInfos(long abZeit, long menge)
        {
            zeitAbstand = abZeit;
            if (isLive) {
                begin = new Date().getTime();
            }
            ende = begin + (abZeit * menge);
            noLimit = false;
        }
    
        public void setInfos(long abZeit)
        {
            if (!isLive) {
                begin = new Date().getTime();
            }
            zeitAbstand = abZeit;
            noLimit = true;
        }
    
        public TimerStatus getStatus()
        {
            getCount();
            return inttoemum(status);
        }
    
        public long getCount()
        {
            long time = new Date().getTime();
            if (isLive) {
                lastTime = time - begin;
                if (time >= ende && !noLimit) {
                    status = enumtoint(TimerStatus.FINISH);
                    isLive = false;
                }
            }
    
            return lastTime / zeitAbstand;
        }
    
        public boolean hasStarted()
        {
            getCount();
            return isLive;
        }
    
        public void start()
        {
            if (!isLive) {
                isLive = true;
                status = enumtoint(TimerStatus.RUNS);
                zeitAbstand = new Date().getTime();
            }
        }
    
        public void restart()
        {
            begin = new Date().getTime();
            status = enumtoint(TimerStatus.RESTARTED);
        }
    
        public void stop()
        {
            if (isLive) {
                isLive = false;
                if (status != enumtoint(TimerStatus.FINISH)) {
                    status = enumtoint(TimerStatus.STOPPED);
                }
            }
        }
    
        private static TimerStatus inttoemum(int i)
        {
            switch (i) {
                case 0:
                    return TimerStatus.NOT_STARTED;
                case 1:
                    return TimerStatus.RUNS;
                case 2:
                    return TimerStatus.FINISH;
                case 3:
                    return TimerStatus.STOPPED;
                case 4:
                default:
                    return TimerStatus.RESTARTED;
            }
        }
    
        private static int enumtoint(TimerStatus i)
        {
            switch (i) {
                case NOT_STARTED:
                    return 0;
                case RUNS:
                    return 1;
                case FINISH:
                    return 2;
                case STOPPED:
                    return 3;
                case RESTARTED:
                default:
                    return 4;
            }
        }
    }
    
    enum TimerStatus implements Serializable
    {
        RESTARTED,
        RUNS,
        NOT_STARTED,
        STOPPED,
        FINISH
    }
    

    leider will er es leider nicht machen und ich versteh nicht warum, es kommt immer eine java.io.NotSerializableException heraus.

    Ich hoffe ihr könnt mir helfen.



  • verschieb mal die enum "TimerStatus" in eine eigene datei. vielleicht liegts daran.



  • Ist TimerStatus (enum) nich ein alias für ein int? brauchst man da da serial interface?

    und bei dem Mapping Methoden könnte man doch auch einfach TimeStartus auf int casten (wenn die reihenfolge) past?



  • NullBockException schrieb:

    Ist TimerStatus (enum) nich ein alias für ein int? brauchst man da da serial interface?

    nein, ein enum ist im grunde eine klasse.


Anmelden zum Antworten