我有以下代码,
equalOr()
但是当我要测试它时,它给了我错误:
public void writeToStream(OutputStream out) {
try {
ObjectOutputStream oos = (ObjectOutputStream) out;
oos.writeObject(root);
oos.close();
} catch (Exception e) {
System.err.println("Caught Exception: " + e.getMessage());
}
}
public void readFromStream(InputStream in) {
try {
ObjectInputStream ois = (ObjectInputStream) in;
root = (Node<T>) ois.readObject();
ois.close();
in.close();
} catch (Exception e) {
System.err.println("Caught Exception: " + e.getMessage());
}
}
它给了我错误:FileOutputStream fos;
try {
fos = new FileOutputStream("object");
ObjectOutputStream oos = new ObjectOutputStream(fos);
tree.writeToStream(oos);
oos.close();
fos.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Exercise4.class.getName()).log(Level.SEVERE, null, ex);
}
BinaryTree<Integer> tree2 = new BinaryTree<Integer>();
FileInputStream fis;
try {
fis = new FileInputStream("Object");
ObjectInputStream ois = new ObjectInputStream(fis);
tree2.readFromStream(ois);
ois.close();
fis.close();
for (int i : tree2) {
System.out.println(i);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Exercise4.class.getName()).log(Level.SEVERE, null, ex);
}
错误是由遍历空对象引起的-从文件读取的对象为空。
写入文件和从文件读取的对象必须相等。
感谢您的帮助。