ObjectOutputStream将Java对象的基本数据类型和图形写入OutputStream。可以使用ObjectInputStream读取(重构)对象。对象的持久存储可以通过使用流的文件来实现。
null
- 仅支持java的对象。伊奥。可序列化接口可以写入流。对每个可序列化对象的类进行编码,包括类的类名和签名、对象的字段和数组的值,以及从初始对象引用的任何其他对象的闭包。
- Java ObjectOutputStream通常与Java ObjectInputStream一起使用。ObjectOutputStream用于写入Java对象,ObjectInputStream用于再次读取对象。
施工人员:
- 受保护的ObjectOutputStream(): 为完全重新实现ObjectOutputStream的子类提供一种方法,使其不必分配ObjectOutputStream的这个实现刚刚使用的私有数据。
- ObjectOutputStream(OutputStream out): 创建写入指定输出流的ObjectOutputStream。
方法:
- 受保护类别(类别cl): 子类可以实现这个方法,以允许类数据存储在流中。默认情况下,此方法不执行任何操作。ObjectInputStream中对应的方法是resolveClass。对于流中的每个唯一类,该方法只调用一次。类名和签名将已经写入流中。此方法可以免费使用ObjectOutputStream来保存它认为合适的类的任何表示(例如,类文件的字节)。ObjectInputStream的相应子类中的resolveClass方法必须读取并使用annotateClass编写的任何数据或对象。
Syntax :protected void annotateClass(Class cl) throws IOExceptionParameters:cl - the class to annotate custom data forThrows:IOException
JAVA
//Java program demonstrating ObjectOutputStream methods //illustrating annotateClass(Class<?> cl) method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super (out); } public static void main(String[] args) throws IOException, ClassNotFoundException { FileOutputStream fout = new FileOutputStream( "file.txt" ); ObjectOutputStreamDemo oot = new ObjectOutputStreamDemo(fout); Character c = 'A' ; //illustrating annotateClass(Class<?> cl) method oot.annotateClass(Character. class ); //Write the specified object to the ObjectOutputStream oot.writeObject(c); //flushing the stream oot.flush(); //closing the stream oot.close(); FileInputStream fin = new FileInputStream( "file.txt" ); ObjectInputStream oit = new ObjectInputStream(fin); System.out.print(oit.readObject()); oit.close(); } } |
输出:
A
- 受保护的代理类(类别cl): 子类可以实现这个方法来在流中存储自定义数据以及动态代理类的描述符。对于流中的每个唯一代理类描述符,该方法只调用一次。ObjectOutputStream中此方法的默认实现不执行任何操作。 ObjectInputStream中对应的方法是resolveProxyClass。对于覆盖此方法的ObjectOutputStream的给定子类,ObjectInputStream的相应子类中的resolveProxyClass方法必须读取由annotateProxyClass编写的任何数据或对象。
Syntax :protected void annotateProxyClass(Class cl) throws IOExceptionParameters:cl - the proxy class to annotate custom data forThrows:IOException
JAVA
//Java program demonstrating ObjectOutputStream //illustrating annotateProxyClass(Class<?> cl) method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super (out); } public static void main(String[] args) throws IOException, ClassNotFoundException { FileOutputStream fout = new FileOutputStream( "file.txt" ); ObjectOutputStreamDemo oot = new ObjectOutputStreamDemo(fout); Character c = 'A' ; //illustrating annotateProxyClass(Class<?> cl) method oot.annotateProxyClass(Character. class ); //Write the specified object to the ObjectOutputStream oot.writeObject(c); //flushing oot.flush(); //closing the stream oot.close(); FileInputStream fin = new FileInputStream( "file.txt" ); ObjectInputStream oit = new ObjectInputStream(fin); System.out.print(oit.readObject()); oit.close(); } } |
输出:
A
- void close(): 关闭小溪。必须调用此方法才能释放与流关联的任何资源。
Syntax :public void close() throws IOExceptionThrows:IOException
JAVA
//Java program demonstrating ObjectOutputStream //illustrating close() method import java.io.*; class ObjectOutputStreamDemo { public static void main(String[] args) throws IOException { FileOutputStream fout = new FileOutputStream( "file.txt" ); ObjectOutputStream oot = new ObjectOutputStream(fout); oot.write( 3 ); //illustrating close() oot.close(); FileInputStream fin = new FileInputStream( "file.txt" ); ObjectInputStream oit = new ObjectInputStream(fin); System.out.println(oit.read()); oit.close(); } } |
- 输出:
3
- void defaultWriteObject(): 将当前类的非静态和非瞬态字段写入此流。这只能从被序列化的类的writeObject方法调用。如果调用NotActivieException,它将抛出NotActivieException。
Syntax :public void defaultWriteObject() throws IOExceptionThrows:IOException
JAVA
//Java program demonstrating ObjectOutputStream //illustrating defaultWriteObject() method import java.io.*; class ObjectOutputStreamDemo { public static void main(String[] arg) throws IOException, ClassNotFoundException { Character a = 'A' ; FileOutputStream fout = new FileOutputStream( "file.txt" ); ObjectOutputStream oot = new ObjectOutputStream(fout); oot.writeChar(a); oot.flush(); // close the stream oot.close(); FileInputStream fin = new FileInputStream( "file.txt" ); ObjectInputStream oit = new ObjectInputStream(fin); // reading the character System.out.println(oit.readChar()); } } class demo implements Serializable { String s = "GeeksfoGeeks" ; private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException { //demonstrating defaultWriteObject() out.defaultWriteObject(); } } } |
输出:
A
- 受保护的空心排水管(): 排空ObjectOutputStream中的所有缓冲数据。与flush类似,但不会将flush传播到底层流。
Syntax :protected void drain() throws IOExceptionThrows:IOException
JAVA
//Java program demonstrating ObjectOutputStream methods //illustrating drain() method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super (out); } public static void main(String[] arg) throws IOException, ClassNotFoundException { FileOutputStream fout = new FileOutputStream( "file.txt" ); ObjectOutputStream oot = new ObjectOutputStream(fout); ObjectOutputStreamDemo obj = new ObjectOutputStreamDemo(oot); //illustrating drain() obj.drain(); //closing the underlying stream oot.close(); fout.close(); } } |
- 受保护的布尔启用替换对象(布尔启用): 使流能够替换流中的对象。启用时,将为每个被序列化的对象调用replaceObject方法。 如果enable为true,并且安装了安全管理器,则此方法首先使用SerializablePermission(“enableSubstitution”)权限调用安全管理器的checkPermission方法,以确保可以启用流来替换流中的对象。
Syntax :protected boolean enableReplaceObject(boolean enable) throws SecurityExceptionParameters:enable - boolean parameter to enable replacement of objectsReturns:the previous setting before this method was invokedThrows:SecurityException
JAVA
//Java program demonstrating ObjectOutputStream //illustrating enableReplaceObject method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super (out); } public static void main(String[] args) throws IOException, ClassNotFoundException { FileOutputStream fout = new FileOutputStream( "file.txt" ); ObjectOutputStreamDemo oot = new ObjectOutputStreamDemo(fout); Character c = 'A' ; //illustrating enableReplaceObject method System.out.println(oot.enableReplaceObject( true )); //Write the specified object to the ObjectOutputStream oot.writeObject(c); //flushing oot.flush(); //closing the stream oot.close(); FileInputStream fin = new FileInputStream( "file.txt" ); ObjectInputStream oit = new ObjectInputStream(fin); System.out.print(oit.readObject()); oit.close(); } } |
输出:
falseA
- ObjectOutputStream。PutField putFields(): 检索用于缓冲要写入流的持久字段的对象。调用writeFields方法时,这些字段将写入流。
Syntax :public ObjectOutputStream.PutField putFields() throws IOExceptionReturns:an instance of the class Putfield that holds the serializable fieldsThrows:IOException
JAVA
//Java program demonstrating ObjectOutputStream //illustrating PutField method import java.io.*; class ObjectOutputStreamDemo { public static void main(String[] arg) throws IOException, ClassNotFoundException { Character a = 'A' ; FileOutputStream fout = new FileOutputStream( "file.txt" ); ObjectOutputStream oot = new ObjectOutputStream(fout); oot.writeChar(a); oot.flush(); // close the stream oot.close(); FileInputStream fin = new FileInputStream( "file.txt" ); ObjectInputStream oit = new ObjectInputStream(fin); // reading the character System.out.println(oit.readChar()); } } class demo implements Serializable { private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException { // Retrieve the object used to buffer // persistent fields to be written to the stream ObjectOutputStream.PutField fields = out.putFields(); } } |
输出:
A
- 受保护对象替换对象(对象obj): 此方法将允许ObjectOutputStream的受信任子类在序列化期间用一个对象替换另一个对象。在调用enableReplaceObject之前,将禁用替换对象。enableReplaceObject方法检查请求进行替换的流是否可信。写入序列化流的每个对象的第一次出现都会传递给replaceObject。对该对象的后续引用将替换为对replaceObject的原始调用返回的对象。为了确保对象的私有状态不会被无意中暴露,只有受信任的流可以使用replaceObject。 当第一次遇到每个对象时,只调用一次此方法。对该对象的所有后续引用都将重定向到新对象。此方法应返回要替换的对象或原始对象。 Null可以作为要替换的对象返回,但在包含对原始对象的引用的类中可能会导致NullReferenceException,因为它们可能期望的是对象而不是Null。
Syntax :protected Object replaceObject(Object obj) throws IOExceptionParameters:obj - the object to be replacedReturns:the alternate object that replaced the specified oneThrows:IOException
JAVA
//Java program demonstrating ObjectOutputStream //illustrating replaceObject method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super (out); } public static void main(String[] args) throws IOException, ClassNotFoundException { FileOutputStream fout = new FileOutputStream( "file.txt" ); ObjectOutputStreamDemo oot = new ObjectOutputStreamDemo(fout); String a = "forGeeks" ; String b = "Geeks" ; //Write the specified object to the ObjectOutputStream oot.writeObject(a); //flushing the stream oot.flush(); oot.enableReplaceObject( true ); //illustrating replaceObject System.out.print(oot.replaceObject(b)); //closing the stream oot.close(); FileInputStream fin = new FileInputStream( "file.txt" ); ObjectInputStream oit = new ObjectInputStream(fin); System.out.print(oit.readObject()); oit.close(); } } |
输出:
GeeksforGeeks
- void useProtocolVersion(int版本): 指定写入流时要使用的流协议版本。此例程提供了一个钩子,使当前版本的序列化能够以向后兼容于流格式以前版本的格式写入。 将尽一切努力避免引入额外的向后不兼容;然而,有时别无选择。
Syntax :public void useProtocolVersion(int version) throws IOExceptionParameters:version - use ProtocolVersion from java.io.ObjectStreamConstants.Throws:IllegalStateException IllegalArgumentExceptionIOException
JAVA
//Java program demonstrating ObjectOutputStream //illustrating useProtocolVersion() method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super (out); } public static void main(String[] args) throws IOException, ClassNotFoundException { FileOutputStream fout = new FileOutputStream( "file.txt" ); ObjectOutputStreamDemo oot = new ObjectOutputStreamDemo(fout); String a = "forGeeks" ; String b = "Geeks" ; //illustrating useProtocolVersion() oot.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2); //Write the specified object to the ObjectOutputStream oot.writeObject(b); oot.writeObject(a); //flushing the stream oot.flush(); oot.close(); FileInputStream fin = new FileInputStream( "file.txt" ); ObjectInputStream oit = new ObjectInputStream(fin); System.out.print(oit.readObject()); System.out.print(oit.readObject()); oit.close(); } } |
输出:
GeeksforGeeks
下一篇文章: JAVA伊奥。Java |集合2中的ObjectOutputStream类
本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END