Java.io.ObjectOutputStream-klass i Java | Set 2
Java.io.ObjectOutputStream-klass i Java | Set 1 Fler metoder:
- Ett objekt skrivet via writeUnshared serialiseras alltid på samma sätt som ett nytt objekt (ett objekt som inte har skrivits till strömmen ännu) oavsett om objektet har skrivits tidigare eller inte.
- Om writeObject används för att skriva ett objekt som tidigare har skrivits med writeUnshared behandlas den tidigare writeUnshared-operationen som om det vore en skrivning av ett separat objekt. Med andra ord kommer ObjectOutputStream aldrig att generera bakåtreferenser till objektdata skrivna av anrop till writeUnshared.
Syntax : public void write(byte[] buf) throws IOException Parameters: buf - the data to be written Throws: IOException
Syntax : public void write(byte[] buf int off int len) throws IOException Parameters: buf - the data to be written off - the start offset in the data len - the number of bytes that are written Throws: IOException
Syntax : public void write(int val) throws IOException Parameters: val - the byte to be written to the stream Throws: IOException
Syntax : public void writeBoolean(boolean val) throws IOException Parameters: val - the boolean to be written Throws: IOException
Syntax : public void writeByte(int val) throws IOException Parameters: val - the byte value to be written Throws: IOException
Syntax : public void writeBytes(String str) throws IOException Parameters: str - the String of writeBytes to be written Throws: IOException
Syntax : public void writeChar(int val) throws IOException Parameters: val - the char value to be written Throws: IOException
Syntax : public void writeChars(String str) throws IOException Parameters: str - the String of chars to be written Throws: IOException
Syntax : protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException Parameters: desc - class descriptor to write to the stream Throws: IOException
Syntax : public void writeDouble(double val) throws IOException Parameters: val - the double value to be written Throws: IOException
Syntax : public void writeFields() throws IOException Throws: IOException NotActiveException
Syntax : public void writeFloat(float val) throws IOException Parameters: val - the float value to be written Throws: IOException
Syntax : public void writeInt(int val) throws IOException Parameters: val - the integer value to be written Throws: IOException
Syntax : public void writeLong(long val) throws IOException Parameters: val - the long value to be written Throws: IOException
Syntax : public final void writeObject(Object obj) throws IOException Parameters: obj - the object to be written Throws: InvalidClassException NotSerializableException IOException
Syntax : protected void writeObjectOverride(Object obj) throws IOException Parameters: obj - object to be written to the underlying stream Throws: IOException
Syntax : public void writeShort(int val) throws IOException Parameters: val - the short value to be written Throws: IOException
Syntax : protected void writeStreamHeader() throws IOException Throws: IOException
Syntax : public void writeUnshared(Object obj) throws IOException Parameters: obj - object to write to stream Throws: NotSerializableException InvalidClassException IOException
Syntax : public void writeUTF(String str) throws IOException Parameters: str - the String to be written Throws: IOException
Syntax : public void flush() throws IOException Throws: IOExceptionProgram: Java
//Java program demonstrating ObjectOutputStream //write methods import java.io.* ; class ObjectOutputStreamDemo { public static void main ( String [] args ) throws IOException ClassNotFoundException { FileOutputStream fout = new FileOutputStream ( 'file.txt' ); ObjectOutputStream oot = new ObjectOutputStream ( fout ); String a = 'GeeksforGeeks' ; String b = 'Geek' ; byte [] be = { 'A' 'B' 'C' }; //illustrating write() oot . write ( 1 ); //illustrating writeInt(int i) oot . writeInt ( 1 ); //illustrating writeBoolean(boolean a) oot . writeBoolean ( true ); //illustrating writeObject(Object x) oot . writeObject ( a ); //illustrating writeByte(byte a) oot . writeByte ( 65 ); //illustrating writeBytes(String b) oot . writeBytes ( b ); //illustrating writeDouble(double d) oot . writeDouble ( 2.3 ); //illustrating writeUTF(String str) oot . writeUTF ( a ); //illustrating writeFloat(float x) oot . writeFloat ( 2.42f ); //illustrating writeLone(long x) oot . writeLong ( 234342347908l ); //illustrating writeChars(String a) oot . writeChars ( a ); //illustrating writeShort(int val) oot . writeShort ( 2 ); //illustrating write(byte[] buff) oot . write ( be ); //flushing the stream oot . flush (); oot . close (); byte c []= new byte [ 4 ] ; char c1 []= new char [ 13 ] ; FileInputStream fin = new FileInputStream ( 'file.txt' ); ObjectInputStream oit = new ObjectInputStream ( fin ); System . out . println ( oit . read ()); System . out . println ( oit . readInt ()); System . out . println ( oit . readBoolean ()); System . out . println ( oit . readObject ()); System . out . println ( oit . readByte ()); oit . read ( c ); for ( int i = 0 ; i < 4 ; i ++ ) { System . out . print (( char ) c [ i ] ); } System . out . println (); System . out . println ( oit . readDouble ()); System . out . println ( oit . readUTF ()); System . out . println ( oit . readFloat ()); System . out . println ( oit . readLong ()); for ( int i = 0 ; i < 13 ; i ++ ) { System . out . print ( oit . readChar ()); } System . out . println (); System . out . println ( oit . readShort ()); oit . readFully ( be ); for ( int i = 0 ; i < 3 ; i ++ ) { System . out . print (( char ) be [ i ] ); } oit . close (); } }
Utgång: 1 1 true GeeksforGeeks 65 Geek 2.3 GeeksforGeeks 2.42 234342347908 GeeksforGeeks 2 ABCProgram 2: Java
//Java program illustrating ObjectOutputStream //write methods import java.io.* ; class ObjectOutputStreamDemo { public static void main ( String [] args ) throws IOException ClassNotFoundException { FileOutputStream out = new FileOutputStream ( 'file.txt' ); ObjectOutputStream oout = new ObjectOutputStream ( out ); oout . writeObject ( new demo ()); //illustrating writeUnshared() //Writes an 'unshared' object to the ObjectOutputStream. oout . writeUnshared ( 14 ); //flush the stream oout . flush (); oout . close (); FileInputStream fin = new FileInputStream ( 'file.txt' ); ObjectInputStream ois = new ObjectInputStream ( fin ); // read an object from the stream and cast it to demo demo obj = ( demo ) ois . readObject (); System . out . println ( obj . var ); System . out . println ( ois . readUnshared ()); } } class demo implements Serializable { static int var = 25; // assign a new serialPersistentFields private static final ObjectStreamField [] serialPersistentFields = { new ObjectStreamField ( 'var' Integer . TYPE ) }; private void readObject ( ObjectInputStream in ) throws IOException ClassNotFoundException { // makes them available by name. ObjectInputStream . GetField fields = in . readFields (); //Get the value of the named int field from the persistent field. var = fields . get ( 'var' 0 ); } private void writeObject ( ObjectOutputStream out ) throws IOException { // write into the ObjectStreamField array the variable string ObjectOutputStream . PutField fields = out . putFields (); fields . put ( 'var' var ); //Write the buffered fields to the stream out . writeFields (); } }
Utgång: 25 14Skapa frågesport