Java I/O 教程(十) ObjectOutputStream和ObjectInputStream
阿新 • • 發佈:2019-02-06
ObjectOutputStream
ObjectOutputStream用於往輸出流中寫入原始型別和Java物件。
類定義
建構函式
protected |
Provide a way for subclasses that are completely reimplementing ObjectOutputStream to not have to allocate private data just used by this implementation of ObjectOutputStream. |
Creates an ObjectOutputStream that writes to the specified OutputStream. |
方法
protected void |
Subclasses may implement this method to allow class data to be stored in the stream. |
protected void |
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 |
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 |
Enable the stream to do replacement of objects in the stream. |
void |
flush()
Flushes the stream.
|
Retrieve the object used to buffer persistent fields to be written to the stream. | |
protectedObject |
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 |
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 |
Writes a boolean. |
void |
Writes an 8 bit byte. |
void |
Writes a String as a sequence of bytes. |
void |
Writes a 16 bit char. |
void |
Writes a String as a sequence of chars. |
protected void |
Write the specified class descriptor to the ObjectOutputStream. |
void |
Writes a 64 bit double. |
void |
Write the buffered fields to the stream. |
void |
Writes a 32 bit float. |
void |
Writes a 32 bit int. |
void |
Writes a 64 bit long. |
void |
Write the specified object to the ObjectOutputStream. |
protected void |
Method used by subclasses to override the default writeObject method. |
void |
Writes a 16 bit short. |
protected void |
The writeStreamHeader method is provided so subclasses can append or prepend their own header to the stream. |
void |
Writes an "unshared" object to the ObjectOutputStream. |
void |
Primitive data write of this String inmodified UTF-8format. |
例子
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物件。
類定義
建構函式
protected |
Provide a way for subclasses that are completely reimplementing ObjectInputStream to not have to allocate private data just used by this implementation of ObjectInputStream. |
Creates an ObjectInputStream that reads from the specified InputStream. |
方法
int |
Returns the number of bytes that can be read without blocking. |
void |
close()
Closes the input stream.
|
void |
Read the non-static and non-transient fields of the current class from this stream. |
protected boolean |
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 |
Reads in a boolean. |
byte |
Reads an 8 bit byte. |
char |
Reads a 16 bit char. |
Read a class descriptor from the serialization stream. | |
double |
Reads a 64 bit double. |
Reads the persistent fields from the stream and makes them available by name. | |
float |
Reads a 32 bit float. |
void |
Reads bytes, blocking until all bytes are read. |
void |
(byte[] buf,
int off, int len)
Reads bytes, blocking until all bytes are read.
|
int |
Reads a 32 bit int. |
Deprecated. This method does not properly convert bytes to characters. see DataInputStream for the details and alternatives. | |
long |
Reads a 64 bit long. |
Read an object from the ObjectInputStream. | |
protectedObject |
This method is called by trusted subclasses of ObjectOutputStream that constructed ObjectOutputStream using the protected no-arg constructor. |
short |
Reads a 16 bit short. |
protected void |
The readStreamHeader method is provided to allow subclasses to read and verify their own stream headers. |
Reads an "unshared" object from the ObjectInputStream. | |
int |
Reads an unsigned 8 bit byte. |
int |
Reads an unsigned 16 bit short. |
void |
Register an object to be validated before the graph is returned. |
protectedClass<?> |
Load the local class equivalent of the specified stream class description. |
protectedObject |
This method will allow trusted subclasses of ObjectInputStream to substitute one object for another during deserialization. |
protectedClass<?> |
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 |
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物件的例子,請參考文章: