Java Java类设置 1
ObjectOutputStream 将Java对象的原始数据类型和图形写入 OutputStream。可以使用 ObjectInputStream 读取(重构)对象。对象的持久存储可以通过使用流的文件来实现。
- 只有支持Java.io.Serializable 接口的对象才能写入流。每个可序列化对象的类都经过编码,包括类的类名和签名、对象的字段和数组的值,以及从初始对象引用的任何其他对象的闭包。
- Java ObjectOutputStream 通常与Java ObjectInputStream 一起使用。 ObjectOutputStream 用于写入Java对象,ObjectInputStream 用于再次读取对象。
构造函数:
- protected ObjectOutputStream() :为完全重新实现 ObjectOutputStream 的子类提供一种方法,使其不必分配此 ObjectOutputStream 实现刚刚使用的私有数据。
- ObjectOutputStream(OutputStream out) :创建一个写入指定 OutputStream 的 ObjectOutputStream。
方法:
- protected void annotateClass(Class cl) :子类可以实现此方法以允许将类数据存储在流中。默认情况下,此方法不执行任何操作。 ObjectInputStream 中对应的方法是resolveClass。对于流中的每个唯一类,该方法只调用一次。类名和签名将已写入流。此方法可以免费使用 ObjectOutputStream 来保存它认为合适的类的任何表示(例如,类文件的字节)。 ObjectInputStream 对应子类中的resolveClass 方法必须读取和使用annotateClass 写入的任何数据或对象。
Syntax :protected void annotateClass(Class cl)
throws IOException
Parameters:
cl - the class to annotate custom data for
Throws:
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();
}
}
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();
}
}
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();
}
}
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();
}
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
输出 :
A
- protected void annotateProxyClass(Class cl) :子类可以实现此方法以将自定义数据与动态代理类的描述符一起存储在流中。对于流中的每个唯一代理类描述符,该方法只调用一次。 ObjectOutputStream 中此方法的默认实现不执行任何操作。
ObjectInputStream 中对应的方法是resolveProxyClass。对于覆盖此方法的给定 ObjectOutputStream 子类,ObjectInputStream 相应子类中的 resolveProxyClass 方法必须读取 annotateProxyClass 写入的任何数据或对象。
Syntax :protected void annotateProxyClass(Class cl)
throws IOException
Parameters:
cl - the proxy class to annotate custom data for
Throws:
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 IOException
Throws:
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 方法中调用。如果以其他方式调用它将抛出 NotActiveException。
Syntax :public void defaultWriteObject()
throws IOException
Throws:
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
- protected void drain() :排出 ObjectOutputStream 中的所有缓冲数据。与刷新类似,但不会将刷新传播到底层流。
Syntax :protected void drain()
throws IOException
Throws:
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();
}
}
- protected boolean enableReplaceObject(boolean enable):使流能够替换流中的对象。启用后,将为每个正在序列化的对象调用 replaceObject 方法。
如果 enable 为 true,并且安装了安全管理器,则该方法首先调用安全管理器的 checkPermission 方法,并具有 SerializablePermission(“enableSubstitution”) 权限,以确保可以启用流来替换流中的对象。
Syntax :protected boolean enableReplaceObject(boolean enable)
throws SecurityException
Parameters:
enable - boolean parameter to enable replacement of objects
Returns:
the previous setting before this method was invoked
Throws:
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();
}
}
输出 :
false
A
- ObjectOutputStream.PutField putFields():检索用于缓冲要写入流的持久字段的对象。当调用 writeFields 方法时,字段将被写入流。
Syntax :public ObjectOutputStream.PutField putFields()
throws IOException
Returns:
an instance of the class Putfield that holds the serializable fields
Throws:
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
- protected Object replaceObject(Object obj):此方法将允许 ObjectOutputStream 的受信任子类在序列化期间用一个对象替换另一个对象。在调用 enableReplaceObject 之前,禁用替换对象。 enableReplaceObject 方法检查请求进行替换的流是否可信。写入序列化流的每个对象的第一次出现被传递给replaceObject。对该对象的后续引用被替换为原始调用replaceObject 返回的对象。为了确保对象的私有状态不会被无意暴露,只有受信任的流可以使用 replaceObject。
此方法仅在第一次遇到每个对象时调用一次。对该对象的所有后续引用都将重定向到新对象。此方法应返回要替换的对象或原始对象。
Null 可以作为要替换的对象返回,但可能会在包含对原始对象的引用的类中导致 NullReferenceException,因为它们可能期望一个对象而不是 null。
Syntax :protected Object replaceObject(Object obj)
throws IOException
Parameters:
obj - the object to be replaced
Returns:
the alternate object that replaced the specified one
Throws:
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 version) :指定写入流时要使用的流协议版本。此例程提供了一个挂钩,使当前版本的序列化能够以向后兼容的流格式的先前版本的格式写入。
将尽一切努力避免引入额外的向后不兼容;但是,有时没有其他选择。
Syntax :public void useProtocolVersion(int version)
throws IOException
Parameters:
version - use ProtocolVersion from java.io.ObjectStreamConstants.
Throws:
IllegalStateException
IllegalArgumentException
IOException
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.io.ObjectOutputStream 类Java |设置 2