The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Long-term persistence is a model that enables beans to be saved in XML format.
Information on the XML format and on how to implement long-term persistence for non-beans can be found in XML Schema and Using XMLEncoder.
The XMLEncoder
class is assigned to write output files for textual representation of Serializable
objects. The following code fragment is an example of writing a Java bean and its properties in XML format:
XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream( new FileOutputStream("Beanarchive.xml"))); encoder.writeObject(object); encoder.close();
The XMLDecoder
class reads an XML document that was created with XMLEncoder:
XMLDecoder decoder = new XMLDecoder( new BufferedInputStream( new FileInputStream("Beanarchive.xml"))); Object object = decoder.readObject(); decoder.close();
An XML bean archive has its own specific syntax, which includes the following tags to represent each bean element:
<java>
tag to embody all object elements of the bean<object>
tag to represent a set of method calls needed to reconstruct an object from its serialized form <object class="javax.swing.JButton" method="new"> <string>Ok</string> </object>
<object class="javax.swing.JButton"> <void method="setText"> <string>Cancel</string> </void> </object>
<boolean>
<byte>
<char>
<short>
<int>
<long>
<float>
<double>
<int>5555</int>
class
> tag to represent an instance of Class. <class>java.swing.JFrame</class>
array
> tag to define an array <array class="java.lang.String" length="5"> </array>
The following code represents an XML archive that will be generated for the SimpleBean
component:
<?xml version="1.0" encoding="UTF-8" ?> <java> <object class="javax.swing.JFrame"> <void method="add"> <object class="java.awt.BorderLayout" field="CENTER"/> <object class="SimpleBean"/> </void> <void property="defaultCloseOperation"> <object class="javax.swing.WindowConstants" field="DISPOSE_ON_CLOSE"/> </void> <void method="pack"/> <void property="visible"> <boolean>true</boolean> </void> </object> </java>