JAVA伊奥。Java |集合1中的ObjectOutputStream类
null
更多方法:
- 无效写入(字节[]buf): 写入字节数组。此方法将一直阻塞,直到实际写入字节。
Syntax :public void write(byte[] buf) throws IOException Parameters: buf - the data to be written Throws: IOException
- 无效写入(字节[]buf,int off,int len): 写入字节的子数组。
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
- 无效写入(int-val): 写入一个字节。此方法将一直阻塞,直到实际写入字节。
Syntax :public void write(int val) throws IOException Parameters: val - the byte to be written to the stream Throws: IOException
- void writebolean(布尔值): 写一个布尔值。
Syntax :public void writeBoolean(boolean val) throws IOException Parameters: val - the boolean to be written Throws: IOException
- 无效写入字节(int val): 写入8位字节。
Syntax :public void writeByte(int val) throws IOException Parameters: val - the byte value to be written Throws: IOException
- 无效写字节(字符串str): 以字节序列的形式写入字符串。
Syntax :public void writeBytes(String str) throws IOException Parameters: str - the String of writeBytes to be written Throws: IOException
- 无效写入卡(int val): 写入16位字符。
Syntax :public void writeChar(int val) throws IOException Parameters: val - the char value to be written Throws: IOException
- 无效的writeChars(字符串str): 将字符串作为字符序列写入。
Syntax :public void writeChars(String str) throws IOException Parameters: str - the String of chars to be written Throws: IOException
- 受保护的void writeClassDescriptor(ObjectStreamClass desc): 将指定的类描述符写入ObjectOutputStream。类描述符用于识别写入流的对象类。ObjectOutputStream的子类可以重写此方法,以自定义将类描述符写入序列化流的方式。然后,应重写ObjectInputStream中的相应方法readClassDescriptor,以从其自定义流表示中重新构造类描述符。默认情况下,此方法根据对象序列化规范中定义的格式编写类描述符。
Syntax :protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException Parameters: desc - class descriptor to write to the stream Throws: IOException
- 无效可写双(双val): 写入64位双精度。
Syntax :public void writeDouble(double val) throws IOException Parameters: val - the double value to be written Throws: IOException
- void writeFields(): 将缓冲字段写入流。
Syntax :public void writeFields() throws IOException Throws: IOException NotActiveException
- 无效写浮动(浮动值): 写入32位浮点。
Syntax :public void writeFloat(float val) throws IOException Parameters: val - the float value to be written Throws: IOException
- 无效写入(int val): 写入32位整数。
Syntax :public void writeInt(int val) throws IOException Parameters: val - the integer value to be written Throws: IOException
- 无效写长(长val): 写入64位长的。
Syntax :public void writeLong(long val) throws IOException Parameters: val - the long value to be written Throws: IOException
- 无效写入对象(对象对象): 将指定的对象写入ObjectOutputStream。写入对象的类、类的签名、类及其所有超类型的非瞬态和非静态字段的值。可以使用writeObject和readObject方法重写类的默认序列化。该对象引用的对象以传递方式写入,因此ObjectInputStream可以重建对象的完整等价图。
Syntax :public final void writeObject(Object obj) throws IOException Parameters: obj - the object to be written Throws: InvalidClassException NotSerializableException IOException
- 受保护的void writeObjectOverride(对象obj): 子类用于重写默认writeObject方法的方法。ObjectInputStream的受信任子类调用此方法,该子类使用受保护的无参数构造函数构造ObjectInputStream。该子类将提供一个带有修饰符“final”的覆盖方法。
Syntax :protected void writeObjectOverride(Object obj) throws IOException Parameters: obj - object to be written to the underlying stream Throws: IOException
- 无效写入端口(int val): 写一个16位的短。
Syntax :public void writeShort(int val) throws IOException Parameters: val - the short value to be written Throws: IOException
- 受保护的void writeStreamHeader(): writeStreamHeader方法是这样提供的,子类可以将自己的头追加或前置到流中。它将魔法数字和版本写入流。
Syntax :protected void writeStreamHeader() throws IOException Throws: IOException
- 无效写入共享(对象对象对象): 将“非共享”对象写入ObjectOutputStream。此方法与writeObject相同,只是它总是将给定对象作为流中新的唯一对象写入(与指向以前序列化的实例的反向引用相反)。明确地:
- 通过writeUnshared写入的对象总是以与新出现的对象(尚未写入流的对象)相同的方式序列化,无论该对象之前是否已写入。
- 如果writeObject用于写入之前使用writeUnshared写入的对象,则之前的writeUnshared操作将被视为对单独对象的写入。换句话说,ObjectOutputStream永远不会生成对writeUnshared调用写入的对象数据的反向引用。
虽然通过writeUnshared写入对象本身并不能保证反序列化时对该对象的唯一引用,但它允许在流中多次定义单个对象,这样接收方对readUnshared的多次调用就不会冲突。请注意,上述规则仅适用于使用writeUnshared编写的基本级别对象,而不适用于要序列化的对象图中的任何传递引用子对象。 重写此方法的ObjectOutputStream子类只能在具有“enableSubclassImplementation”SerializablePermission的安全上下文中构造;任何未经此权限实例化此类子类的尝试都将导致抛出SecurityException。
Syntax :public void writeUnshared(Object obj) throws IOException Parameters: obj - object to write to stream Throws: NotSerializableException InvalidClassException IOException
- void writeUTF(字符串str): 以修改的UTF-8格式写入此字符串的基本数据。请注意,将字符串作为基本数据或对象写入流中有很大区别。writeObject编写的字符串实例最初作为字符串写入流中。Future writeObject()调用将对字符串的引用写入流中。
Syntax :public void writeUTF(String str) throws IOException Parameters: str - the String to be written Throws: IOException
- 无效刷新(): 冲水。这将写入任何缓冲输出字节,并刷新到底层流。
Syntax :public void flush() throws IOException Throws: IOException
节目:
//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(); } } |
输出:
1 1 true GeeksforGeeks 65 Geek 2.3 GeeksforGeeks 2.42 234342347908 GeeksforGeeks 2 ABC
项目2:
//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(); } } |
输出:
25 14
本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END