-2

How can i fix problem with load ArrayList from file?

Full code is on the git: project github link

When I load data from file I'll get it back -> IOException

I'm learning to use the stream so I'm writing it to a file.

After the end of the program, I want to write the time the user has reached in the arraylist and list all the times with which the game was finished.

package sk.games.puzzle;

import java.io.*;
import java.util.*;

public class BestTimes implements Iterable<BestTimes.PlayerTime>, Serializable{

    private static final String BESTTIME_DB = System.getProperty("user.home")
            + System.getProperty("file.separator")
            + "best.time";

    private List<PlayerTime> playerTimes = new ArrayList<>();

    public Iterator<PlayerTime> iterator() {
        return playerTimes.iterator();
    }

    public void addTime(String name, int time){
        playerTimes.add(new PlayerTime(name, time));
        Collections.sort(playerTimes);
    }

    public void load(){
        ObjectInputStream load = null;
        try {
            load = new ObjectInputStream(new FileInputStream(BESTTIME_DB));
            playerTimes = (ArrayList<PlayerTime>) load.readObject();
        } catch (FileNotFoundException e) {
            System.err.println("fail nebola najdena db");
        } catch (IOException e) {
            System.err.println("fail nebola otvorena db");
        } catch (ClassNotFoundException e) {
            System.err.println("fail nebol najdeny zaznam");
        } finally {
            if (load != null) {
                try {
                    load.close();
                } catch (IOException e) {
                    //empty
                }
            }
        }
    }

    public void save() {
        ObjectOutputStream save = null;
        try {
            save = new ObjectOutputStream(new FileOutputStream(BESTTIME_DB));
            save.writeObject(playerTimes);
        } catch (FileNotFoundException e) {
            System.err.println("fail db neexistuje");
        } catch (IOException e) {
            System.err.println("fail nepodarilo sa otvorit db");
        } finally {
            if (save != null) {
                try {
                    save.close();
                } catch (IOException e) {
                    //empty
                }
            }
        }
    }

    @Override
    public String toString() {
        Formatter f = new Formatter();
        for (int i = 0; i < playerTimes.size(); i++) {
            PlayerTime pt = playerTimes.get(i);
            f.format("%02d. %s - %ds.\n", i, pt.getName(), pt.getTime());
        }
        return f.toString();
    }

    public static class PlayerTime implements Comparable<PlayerTime> {

        private final String name;
        private final int time;

        public PlayerTime(String name, int time) {
            this.name = name;
            this.time = time;
        }

        public String getName() {
            return name;
        }

        public int getTime() {
            return time;
        }

        @Override
        public int compareTo(PlayerTime o){
            return Integer.compare(this.time, o.getTime());
        }
    }
}
  • 2
    and (almost) NEVER EVER ignore exceptions, like is being done in the `save` method!!! (`catch IOException e) { // empty`) - How you know it saved anything? – user16320675 Jul 24 '22 at 15:38
  • 1
    The purpose of exceptions is to tell you what went wrong and where. You are discarding that that valuable information. A `catch` block should always print or log an exception’s full stack trace (or wrap it in another thrown exception). In this case, you can learn a lot about the problem by adding `e.printStackTrace();` to each of your catch blocks. – VGR Jul 24 '22 at 17:03

1 Answers1

0

The problem is that your PlayerTime class is not serializable.

public static class PlayerTime implements Comparable<PlayerTime> { }

should be

public static class PlayerTime implements Comparable<PlayerTime> implements Serializable { }

It's not necessary to make BestTimes serializable unless you do write BestTimes object to file.

Cheng Thao
  • 1,467
  • 1
  • 3
  • 9