序列化不起作用

时间:2017-04-27 22:56:24

标签: java serialization

我正在尝试使用Serializable接口保存类实例。 我在打开保存的文件并将其分配给对象变量时遇到了一些麻烦。

这是我的代码:

主要

imports...

public class KBCalculator {
    protected static ActorGraph graph;

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        graph = new ActorGraph();
        InputStreamReader inStreamR = new InputStreamReader(System.in);
        BufferedReader input = new BufferedReader(inStreamR);   

        try {
            FileInputStream fileIn = new FileInputStream("Imports.txt");
            ObjectInputStream inStream = new ObjectInputStream(fileIn);
            graph = (ActorGraph) inStream.readObject();
            inStream.close();
        } catch (FileNotFoundException e) { }

        System.out.println("------------------------------\n"
                         + "       Welcome to KBC!\n"
                         + "------------------------------\n");

        while(true) {
            System.out.print("Menu:\n------------------------------\n\n"
                           + "  I: Import a Movie\n"
                           + "  A: Print all Actors\n"
                           + "  M: Print all Movies\n"
                           + "  P: Print the shortest path between two actors\n"
                           + "  B: Print the BFS (Breadth First Search) from a given actor\n"
                           + "  L: Lookup Actor By Name\n"
                           + "  Q: Quit\n"
                           + "\n------------------------------\n");

            System.out.print("\nEnter option: ");
            String choice = input.readLine().toUpperCase();

            switch(choice) {
                case "I": importMovie();
                    break;
                case "A" : printActors();
                    break;
                case "M" : printMovies();
                    break;
                case "P" : printShortestPath();
                    break;
                case "B" : printBFS();
                    break;
                case "L" : lookupActor();
                    break;
                case "Q" : FileOutputStream fileOut = new FileOutputStream("Imports.txt");
                    ObjectOutputStream outStream = new ObjectOutputStream(fileOut);
                    outStream.writeObject(graph);
                    outStream.close();

                    System.out.println("\nGoodbye!");
                    System.exit(0);

                    break;
                default: System.out.println("\nInvalid Input. Try Again.\n");
                    break;
            }
        }
    }
}

public static void importMovie() {
    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    String title = "";
    Movie movie;

    System.out.print("\nEnter the movie title: ");
    title = input.nextLine();

    if(!graph.getMoviesByTitle().containsKey(title)) {
        movie = new Movie(title);
    } else {
        System.out.println("\nMove has already been imported!\n");
        return;
    }

    graph.setMoviesByTitle(title, movie);

    System.out.println(movie.getTitle() + " Successfully Imported!\n");
}

ActorGraph.java

imports...

public class ActorGraph implements Serializable {

    private static HashMap<String,Actor> actorsByName;
    private static HashMap<String, Movie> moviesByTitle;

    public ActorGraph() {
        actorsByName = new HashMap<String, Actor>();
        moviesByTitle = new HashMap<String, Movie>();
    }

... other methods

使用图形实例将影片导入ActorGraph哈希图后,使用eclipse调试器检查以确保导入影片。然后,我退出程序并将对象保存在文件中。

在关闭程序之前,图形实例的变量是非空的。

重启程序后;该文件被放入构造的图形变量中。问题是,现在,导入的电影已经消失。这意味着我尝试保存到文件的实例不起作用。图形实例的变量都是null。我无法弄清楚原因。

谢谢

1 个答案:

答案 0 :(得分:0)

  

图形实例的变量都是空的。

ActorGraph 它只有静态变量。

  

我无法弄清楚原因。

因为静态成员未被序列化。

让它们变得非静态。