Java I/O 教程(十) ObjectOutputStream和ObjectInputStream
ObjectOutputStream
ObjectOutputStream用於往輸出流中寫入原始型別和Java物件。
類定義
public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants
建構函式
protected |
ObjectOutputStream()
Provide a way for subclasses that are completely reimplementing ObjectOutputStream to not have to allocate private data just used by this implementation of ObjectOutputStream.
|
ObjectOutputStream(OutputStream out)
Creates an ObjectOutputStream that writes to the specified OutputStream.
|
方法
protected void |
annotateClass(Class<?> cl)
Subclasses may implement this method to allow class data to be stored in the stream.
|
protected void |
annotateProxyClass(Class<?> cl)
Subclasses may implement this method to store custom data in the stream along with descriptors for dynamic proxy classes.
|
void |
close()
Closes the stream.
|
void |
defaultWriteObject()
Write the non-static and non-transient fields of the current class to this stream.
|
protected void |
drain()
Drain any buffered data in ObjectOutputStream.
|
protected boolean |
enableReplaceObject(boolean enable)
Enable the stream to do replacement of objects in the stream.
|
void |
flush()
Flushes the stream.
|
ObjectOutputStream.PutField |
putFields()
Retrieve the object used to buffer persistent fields to be written to the stream.
|
protected Object |
replaceObject(Object obj)
This method will allow trusted subclasses of ObjectOutputStream to substitute one object for another during serialization.
|
void |
reset()
Reset will disregard the state of any objects already written to the stream.
|
void |
useProtocolVersion(int version)
Specify stream protocol version to use when writing the stream.
|
void |
write(byte[] buf)
Writes an array of bytes.
|
void |
write(byte[] buf,
int off, int len)
Writes a sub array of bytes.
|
void |
write(int val)
Writes a byte.
|
void |
writeBoolean(boolean val)
Writes a boolean.
|
void |
writeByte(int val)
Writes an 8 bit byte.
|
void |
writeBytes(String str)
Writes a String as a sequence of bytes.
|
void |
writeChar(int val)
Writes a 16 bit char.
|
void |
writeChars(String str)
Writes a String as a sequence of chars.
|
protected void |
writeClassDescriptor(ObjectStreamClass desc)
Write the specified class descriptor to the ObjectOutputStream.
|
void |
writeDouble(double val)
Writes a 64 bit double.
|
void |
writeFields()
Write the buffered fields to the stream.
|
void |
writeFloat(float val)
Writes a 32 bit float.
|
void |
writeInt(int val)
Writes a 32 bit int.
|
void |
writeLong(long val)
Writes a 64 bit long.
|
void |
writeObject(Object obj)
Write the specified object to the ObjectOutputStream.
|
protected void |
writeObjectOverride(Object obj)
Method used by subclasses to override the default writeObject method.
|
void |
writeShort(int val)
Writes a 16 bit short.
|
protected void |
writeStreamHeader()
The writeStreamHeader method is provided so subclasses can append or prepend their own header to the stream.
|
void |
writeUnshared(Object obj)
Writes an "unshared" object to the ObjectOutputStream.
|
void |
writeUTF(String str)
|
例子
FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(12345);
oos.writeObject("Today");
oos.writeObject(new Date());
oos.close();
ObjectInputStream
ObjectInputStream用於從輸入流讀取Java物件。
類定義
public class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants
建構函式
protected |
ObjectInputStream()
Provide a way for subclasses that are completely reimplementing ObjectInputStream to not have to allocate private data just used by this implementation of ObjectInputStream.
|
ObjectInputStream(InputStream in)
Creates an ObjectInputStream that reads from the specified InputStream.
|
方法
int |
available()
Returns the number of bytes that can be read without blocking.
|
void |
close()
Closes the input stream.
|
void |
defaultReadObject()
Read the non-static and non-transient fields of the current class from this stream.
|
protected boolean |
enableResolveObject(boolean enable)
Enable the stream to allow objects read from the stream to be replaced.
|
int |
read()
Reads a byte of data.
|
int |
read(byte[] buf,
int off, int len)
Reads into an array of bytes.
|
boolean |
readBoolean()
Reads in a boolean.
|
byte |
readByte()
Reads an 8 bit byte.
|
char |
readChar()
Reads a 16 bit char.
|
protected ObjectStreamClass |
readClassDescriptor()
Read a class descriptor from the serialization stream.
|
double |
readDouble()
Reads a 64 bit double.
|
ObjectInputStream.GetField |
readFields()
Reads the persistent fields from the stream and makes them available by name.
|
float |
readFloat()
Reads a 32 bit float.
|
void |
readFully(byte[] buf)
Reads bytes, blocking until all bytes are read.
|
void |
readFully(byte[] buf,
int off, int len)
Reads bytes, blocking until all bytes are read.
|
int |
readInt()
Reads a 32 bit int.
|
String |
readLine()
Deprecated.
This method does not properly convert bytes to characters. see DataInputStream for the details and alternatives.
|
long |
readLong()
Reads a 64 bit long.
|
Object |
readObject()
Read an object from the ObjectInputStream.
|
protected Object |
readObjectOverride()
This method is called by trusted subclasses of ObjectOutputStream that constructed ObjectOutputStream using the protected no-arg constructor.
|
short |
readShort()
Reads a 16 bit short.
|
protected void |
readStreamHeader()
The readStreamHeader method is provided to allow subclasses to read and verify their own stream headers.
|
Object |
readUnshared()
Reads an "unshared" object from the ObjectInputStream.
|
int |
readUnsignedByte()
Reads an unsigned 8 bit byte.
|
int |
readUnsignedShort()
Reads an unsigned 16 bit short.
|
String |
readUTF()
|
void |
registerValidation(ObjectInputValidation obj,
int prio)
Register an object to be validated before the graph is returned.
|
protected Class<?> |
resolveClass(ObjectStreamClass desc)
Load the local class equivalent of the specified stream class description.
|
protected Object |
resolveObject(Object obj)
This method will allow trusted subclasses of ObjectInputStream to substitute one object for another during deserialization.
|
protected Class<?> |
resolveProxyClass(String[] interfaces)
Returns a proxy class that implements the interfaces named in a proxy class descriptor; subclasses may implement this method to read custom data from the stream along with the descriptors for dynamic
proxy classes, allowing them to use an alternate loading mechanism for the interfaces and the proxy class.
|
int |
skipBytes(int len)
Skips bytes.
|
例子
FileInputStream fis = new FileInputStream("t.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
int i = ois.readInt();
String today = (String) ois.readObject();
Date date = (Date) ois.readObject();
ois.close();
關於讀取Java物件的例子,請參考文章:
Java序列化(Serializable)與反序列化詳解
相關文章
- Java IO: 序列化與ObjectInputStream、ObjectOutputStreamJavaObject
- Java IO框架總攬–ObjectInputStream& ObjectOutputStream原始碼解讀Java框架Object原始碼
- [java IO流]之 序列化流和反序列化流(ObjectOutputStream/ObjectInputStream)JavaObject
- Java I/O 教程(七) DataOutputStream和DataInputStreamJavaAI
- Java I/O 教程(八) Writer和ReaderJava
- Java I/O 教程(九) FileWriter和FileReaderJava
- Java™ 教程(命令列I/O)Java命令列
- Java I/O 教程(二) 介紹OutputStream 和 InputStreamJava
- Java I/O 教程(一) 介紹Java
- Java I/O 教程(四) FileInputStream 類Java
- Java I/O 教程(五) BufferedOutputStream 類Java
- Java I/O 教程(六) BufferedInputStream 類Java
- Java I/O 教程(三) FileOutputStream類Java
- 【java】ObjectOutputStream & ObjectInputStream 多次寫入發生重複寫入相同資料的問題JavaObject
- Java I/OJava
- Java(8)I/OJava
- 【java】I/O流Java
- Java I/O流Java
- java的I/OJava
- I/O埠和I/O記憶體記憶體
- Python教程:精簡概述I/O模型與I/O操作Python模型
- Java 非同步 I/OJava非同步
- JAVA I/O系統Java
- Java基礎——I/O流Java
- java 淺析I/O模型Java模型
- 五種I/O模型和Java NIO原始碼分析模型Java原始碼
- Java中I/O流:阻塞和非阻塞範例Java
- 深入理解Java I/O模型Java模型
- "萬字" Java I/O 詳解Java
- Java I/O 模型的演進Java模型
- Java NIO:淺析I/O模型Java模型
- Java基礎I/O流型別Java型別
- Java I/O系統學習系列一:File和RandomAccessFileJavarandomMac
- Veritas Quick I/O and Cached Quick I/OUI
- Java I/O模型及其底層原理Java模型
- Java學習筆記之I/OJava筆記
- Java入門學習-理解I/OJava
- Java NIO1:I/O模型概述Java模型