java.lang.Object | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | - | - | java.io.InputStream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | - | - | java.io.ObjectInputStream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.
Constructor | Summary |
---|---|
ObjectInputStream(InputStream in) | Creates an ObjectInputStream that reads from the specified InputStream. |
Return | Method | Summary |
---|---|---|
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. |
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. |
double | readDouble() | Reads a 64 bit double. |
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. |
short | readShort() | Reads a 16 bit short. |
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() | Reads a String in modified UTF-8 format. |
void | registerValidation(ObjectInputValidation obj, int prio) | Register an object to be validated before the graph is returned. |
int | skipBytes(int len) | Skips bytes. |
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
public class Main {
static final String dataFile = "data.dat";
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
out.writeObject(new BigInteger("123"));
out.writeInt(123);
out.writeUTF("a String");
} finally {
out.close();
}
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
try {
while (true) {
BigInteger price = (BigInteger) in.readObject();
int unit = in.readInt();
String desc = in.readUTF();
System.out.println(price);
System.out.println(unit);
System.out.println(desc);
}
} catch (EOFException e) {
}
} finally {
in.close();
}
}
}
The output:
123
123
a String
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |