Extras din curs
Clasa ObjectInputStream
Constructor
public ObjectInputStream( java.io.InputStream in )
throws java.io.IOException, java.io.StreamCorruptedException
Create an ObjectInputStream that reads from the specified InputStream. The stream header containing the magic number and version number are read from the stream and verified. This method will block until the corresponding ObjectOutputStream has written and flushed the header.
Metode
int available( ) throws java.io.IOException
void close( ) throws java.io.IOException
final void defaultReadObject( )
throws java.io.IOException, java.lang.ClassNotFoundException, java.io.NotActiveException
int read( bytest data, int offset, int length )
throws java.io.IOException
int read( ) throws java.io.IOException
boolean readBoolean( ) throws java.io.IOException
byte readByte( ) throws java.io.IOException
char readChar( ) throws java.io.IOException
double readDouble( ) throws java.io.IOException
float readFloat( ) throws java.io.IOException
void readFully( bytest data ) throws java.io.IOException
void readFully( bytest data, int offset, int size )
throws java.io.IOException
int readInt( ) throws java.io.IOException
java.lang.String readLine( ) throws java.io.IOException
long readLong( ) throws java.io.IOException
final java.lang.Object readObject( )
throws java.io.OptionalDataException, java.lang.ClassNotFoundException, java.io.IOException
short readShort( ) throws java.io.IOException
int readUnsignedByte( ) throws java.io.IOException
int readUnsignedShort( ) throws java.io.IOException
java.lang.String readUTF( ) throws java.io.IOException
ynchronized void registerValidation( java.io.ObjectInputValidation obj,
int prio )
throws java.io.NotActiveException, java.io.InvalidObjectException
int skipBytes( int len ) throws java.io.IOException
Cuvintul cheie transient
Pentru ca un anumit câmp sa nu fie salvat în urma serializarii, acesta trebuie declarat cu modificatorul transient. Aceste câmpuri vor fi ignorate de metodele writeObject si readObject.
Ex:
private transient x; ® ignorat la serializare
Exemplu:
import java.io.*;
public class TestSerial {
static MyObject obj;
public static void main(String argsst) {
obj = new MyObject(10,20);
try {
FileOutputStream fout = new
FileOutputStream("fisier.tmp");
ObjectOutputStream sout = new
ObjectOutputStream(fout);
sout.writeObject(obj);
sout.flush();
sout.close();
fout.close();
System.out.println("A fost salvat obiectul " + obj);
}
catch (IOException e) {}
System.out.println("Restauram...");
try {
FileInputStream fin = new
FileInputStream("fisier.tmp");
ObjectInputStream sin = new ObjectInputStream(fin);
try {
obj = (MyObject) sin.readObject();
}
catch (ClassNotFoundException e) {}
sin.close();
fin.close();
System.out.println("A fost restaurat obiectul " + obj);
}
catch (IOException e) {}
}
}
class MyObject implements Serializable {
int x;
private transient int y;
public MyObject(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return new String("x=" + x + ", y="+y);
}
}
Rezultatul acestui program va fi :
A fost salvat obiectul x=10, y=20
Restauram...
A fost restaurat obiectul x=10, y=0
Obs. Atunci când o clasa serializabila deriva dintr-o alta clasa, salvarea câmpurilor clasei parinte se va face doar daca si aceasta este serializabila. In caz contrar, subclasa trebuie sa salveze explicit si câmpurile mostenite.
Preview document
Conținut arhivă zip
- Serializarea obiectelor in Java.doc