Java 可序列化文件不工作
我正在尝试使用Serializable接口保存一个类实例。 打开保存的文件并将其分配给对象变量时遇到问题 这是我的代码: MAIN:Java 可序列化文件不工作,java,serialization,Java,Serialization,我正在尝试使用Serializable接口保存一个类实例。 打开保存的文件并将其分配给对象变量时遇到问题 这是我的代码: MAIN: imports... public class KBCalculator { protected static ActorGraph graph; public static void main(String[] args) throws IOException, ClassNotFoundException { graph =
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实现了可序列化{
私有静态HashMap actorsByName;
私有静态HashMap moviesByTitle;
公共ActorGraph(){
actorsByName=newHashMap();
moviesByTitle=新HashMap();
}
…其他方法
使用graph实例将电影导入ActorGraph hashmap后,我使用eclipse调试器检查以确保电影已导入。然后,我退出程序并将对象保存在文件中
关闭程序之前,图形实例的变量不为null
重新启动程序后,该文件被放入构造的图形变量中。问题是,现在导入的电影不见了。这意味着我试图保存到文件中的实例不起作用。图形实例的变量都为空。我不明白为什么
多谢各位
图形实例的变量都为null
ActorGraph
没有任何实例变量。它只有静态变量
我不明白为什么
因为静态成员没有序列化
使它们成为非静态的