Java의 Java.io.ObjectOutputStream 클래스 | 세트 2

Java의 Java.io.ObjectOutputStream 클래스 | 세트 1 더 많은 방법:
    무효 쓰기(바이트[] buf) : Writes an array of bytes. This method will block until the byte is actually written.
      Syntax :  public void write(byte[] buf) throws IOException   Parameters:   buf - the data to be written   Throws:   IOException  
    void write(byte[] buf int off int len) : Writes a sub array of bytes.
      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) : Writes a byte. This method will block until the byte is actually written.
      Syntax :  public void write(int val) throws IOException   Parameters:   val - the byte to be written to the stream   Throws:   IOException  
    void writeBoolean(boolean val) : Writes a boolean.
      Syntax :  public void writeBoolean(boolean val) throws IOException   Parameters:   val - the boolean to be written   Throws:   IOException  
    무효 writeByte(int val) : Writes an 8 bit byte.
      Syntax :  public void writeByte(int val) throws IOException   Parameters:   val - the byte value to be written   Throws:   IOException 
    무효 writeBytes(문자열 str) : Writes a String as a sequence of bytes.
      Syntax :  public void writeBytes(String str) throws IOException   Parameters:   str - the String of writeBytes to be written   Throws:   IOException 
    무효 writeChar(int val) : Writes a 16 bit char.
      Syntax :  public void writeChar(int val) throws IOException   Parameters:   val - the char value to be written   Throws:   IOException 
    무효 writeChars(문자열 str) : Writes a String as a sequence of chars.
      Syntax :  public void writeChars(String str) throws IOException   Parameters:   str - the String of chars to be written   Throws:   IOException 
    protected void writeClassDescriptor(ObjectStreamClass desc) : Write the specified class descriptor to the ObjectOutputStream. Class descriptors are used to identify the classes of objects written to the stream. Subclasses of ObjectOutputStream may override this method to customize the way in which class descriptors are written to the serialization stream. The corresponding method in ObjectInputStream readClassDescriptor should then be overridden to reconstitute the class descriptor from its custom stream representation. By default this method writes class descriptors according to the format defined in the Object Serialization specification.
      Syntax :  protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException   Parameters:   desc - class descriptor to write to the stream   Throws:   IOException 
    void writeDouble(double val) : Writes a 64 bit double.
      Syntax :  public void writeDouble(double val) throws IOException   Parameters:   val - the double value to be written   Throws:   IOException 
    무효 writeFields() : Write the buffered fields to the stream.
      Syntax :  public void writeFields() throws IOException   Throws:   IOException NotActiveException 
    무효 writeFloat(float val) : Writes a 32 bit float.
      Syntax :  public void writeFloat(float val) throws IOException   Parameters:   val - the float value to be written   Throws:   IOException  
    무효 writeInt(int val) : Writes a 32 bit int.
      Syntax :  public void writeInt(int val) throws IOException   Parameters:   val - the integer value to be written   Throws:   IOException  
    무효 writeLong(long val) : Writes a 64 bit long.
      Syntax :  public void writeLong(long val) throws IOException   Parameters:   val - the long value to be written   Throws:   IOException  
    무효 writeObject(객체 obj) : Write the specified object to the ObjectOutputStream.The class of the object the signature of the class and the values of the non-transient and non-static fields of the class and all of its supertypes are written. Default serialization for a class can be overridden using the writeObject and the readObject methods. Objects referenced by this object are written transitively so that a complete equivalent graph of objects can be reconstructed by an ObjectInputStream.
      Syntax :  public final void writeObject(Object obj) throws IOException   Parameters:   obj - the object to be written   Throws:   InvalidClassException NotSerializableException IOException 
    protected void writeObjectOverride(Object obj) : Method used by subclasses to override the default writeObject method. This method is called by trusted subclasses of ObjectInputStream that constructed ObjectInputStream using the protected no-arg constructor. The subclass is expected to provide an override method with the modifier 'final'.
      Syntax :  protected void writeObjectOverride(Object obj) throws IOException   Parameters:   obj - object to be written to the underlying stream   Throws:   IOException  
    무효 writeShort(int val) : Writes a 16 bit short.
      Syntax :  public void writeShort(int val) throws IOException   Parameters:   val - the short value to be written   Throws:   IOException 
    보호된 무효 writeStreamHeader(): The writeStreamHeader method is provided so subclasses can append or prepend their own header to the stream.It writes the magic number and version to the stream.
      Syntax :  protected void writeStreamHeader() throws IOException   Throws:   IOException 
    void writeUnshared(Object obj) : ObjectOutputStream에 '비공유' 개체를 씁니다. 이 메소드는 지정된 객체를 항상 스트림의 새로운 고유 객체로 기록한다는 점을 제외하면 writeObject와 동일합니다(이전에 직렬화된 인스턴스를 가리키는 역참조와 반대). 구체적으로:
    • writeUnshared를 통해 작성된 객체는 해당 객체가 이전에 기록되었는지 여부에 관계없이 새로 나타나는 객체(아직 스트림에 기록되지 않은 객체)와 동일한 방식으로 항상 직렬화됩니다.
    • writeObject를 사용하여 이전에 writeUnshared로 작성된 객체를 쓰는 경우 이전 writeUnshared 작업은 별도의 객체 쓰기인 것처럼 처리됩니다. 즉, ObjectOutputStream은 writeUnshared 호출로 작성된 객체 데이터에 대한 역참조를 생성하지 않습니다.
    While writing an object via writeUnshared does not in itself guarantee a unique reference to the object when it is deserialized it allows a single object to be defined multiple times in a stream so that multiple calls to readUnshared by the receiver will not conflict. Note that the rules described above only apply to the base-level object written with writeUnshared and not to any transitively referenced sub-objects in the object graph to be serialized. ObjectOutputStream subclasses which override this method can only be constructed in security contexts possessing the 'enableSubclassImplementation' SerializablePermission; any attempt to instantiate such a subclass without this permission will cause a SecurityException to be thrown.
      Syntax :  public void writeUnshared(Object obj) throws IOException   Parameters:   obj - object to write to stream   Throws:   NotSerializableException InvalidClassException IOException  
    무효 writeUTF(문자열 str) : Primitive data write of this String in modified UTF-8 format.Note that there is a significant difference between writing a String into the stream as primitive data or as an Object. A String instance written by writeObject is written into the stream as a String initially. Future writeObject() calls write references to the string into the stream.
      Syntax :  public void writeUTF(String str) throws IOException   Parameters:   str - the String to be written   Throws:   IOException 
    무효 플러시() : Flushes the stream. This will write any buffered output bytes and flush through to the underlying stream.
      Syntax :  public void flush() throws IOException   Throws:   IOException 
프로그램 : 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  ();      }   }   
출력 :
1 1 true GeeksforGeeks 65 Geek 2.3 GeeksforGeeks 2.42 234342347908 GeeksforGeeks 2 ABC 
프로그램 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  ();      }   }   
출력 :
25 14 
퀴즈 만들기