java - 我在序列化方面遇到问题,我需要用java实现它

标签 java serialization

我创建了一个程序,它是一个猜谜游戏,但我在学习这种新方法来序列化它时遇到问题。我已经完成了所有工作,我只需要制作它,以便程序可以通过序列化方法保存和加载打开。我正在尝试序列化程序的“游戏”部分或 play() 方法,以便下次加载时它将加载旧信息。

 import javax.swing.JOptionPane;
 import java.io.Serializable;
 import java.io.ObjectOutputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;

 //Node class
 class Node
{
//instance variables
public String questionText;
public Node leftChild;
public Node rightChild;

public void displayText()
{
    System.out.println(questionText);
}
 }


  //Tree class
 class Tree implements Serializable
 {
private Node root;

//constructor
public Tree()
{   root = new Node();
    root.leftChild = new Node();
    root.rightChild = new Node();
    root.questionText = "Does it live on land?";
    root.leftChild.questionText ="bear";  // left side is Yes, right side is No
    root.rightChild.questionText = "parrot";
}

public void instruction()
{
    JOptionPane.showMessageDialog(null, "Think of an animal, I will try to guess it, answer yes or no");
}


public void play()
{
 Node current = root;
 Node parent = current;
 boolean isLeftChild = true;


 while(true)
 {   parent = current;
     int response = JOptionPane.showConfirmDialog(null,current.questionText );
     //code here for yes
     if (response == JOptionPane.YES_OPTION)
     {
        current = current.leftChild;
        isLeftChild=true;
     }
     //code here for no
     else if (response == JOptionPane.NO_OPTION)
     {
        current = current.rightChild;
        isLeftChild = false;
     }


     if (current.leftChild == null && current.rightChild == null)
     {
         int secondQ = JOptionPane.showConfirmDialog(null, "Is your animal a " + current.questionText + "?");

         if (secondQ == JOptionPane.YES_OPTION) 
         {
           JOptionPane.showMessageDialog(null,"I Guessed your animal!");
           return;
         }
         else if (secondQ == JOptionPane.NO_OPTION)
         {
             Node nodeOne = new Node();
             Node nodeTwo = new Node();

              nodeOne.questionText = JOptionPane.showInputDialog("Write a question that differentiates your animal from the animal I guessed, it would be yes for your animal");

              nodeTwo.questionText = JOptionPane.showInputDialog("What is this animal?");

              nodeOne.rightChild = current;
              nodeOne.leftChild = nodeTwo;

              // parent.leftChild = nodeOne or parent.rightChild = nodeOne
              if(isLeftChild == false)
              {
                  parent.rightChild = nodeOne;
                  System.out.println("right child");
              }
              else
                  {
                  parent.leftChild = nodeOne;
                  System.out.println("left Child");
                  }
              return;




         }


     }

}
}


public void preOrder(Node localRoot)
{
if(localRoot != null)
   {
   System.out.print(localRoot.questionText + " ");
   preOrder(localRoot.leftChild);
   preOrder(localRoot.rightChild);
   }
}

public Node getRoot(){
    return root;
}
}




public class GuessTheAnimal 
{
  public static void main(String[] args)
 {
  Tree animal = new Tree();
  animal.instruction();
  animal.play();
  animal.play();



 }

}

最佳答案

我建议Java serialization :

FileOutputStream fileOut =
new FileOutputStream("node.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(tree);
out.close();

阅读同样容易:

FileInputStream fileIn = new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
tree = (Tree) in.readObject();
in.close();

关于java - 我在序列化方面遇到问题,我需要用java实现它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9932341/

相关文章:

java - 使用 REST 和 JSON Web token 上传/下载文件

php - 检查字符串是否在 PHP 中序列化

c++ - boost C++ 序列化/反序列化

c# - 数据数组的自定义序列化

java.io.NotSerializedException : com. mysql.jdbc.DatabaseMetaData

java - 递归归并排序Java程序

java - HttpSecurity 正则表达式匹配器未按预期工作

java - 如何从不同的文件访问名为 "numberPart"的字符串(多线程java套接字编程)

java - 方法不存在 (MainActivity.java)

c# - Protobuf-net 尝试 JIT 编译方法 '(wrapper dynamic-method) ClassExtensions.StringArray