Open In App

Java.io.ObjectOutputStream Class in Java | Set 2

Java.io.ObjectOutputStream Class in Java | Set 1

More Methods:



Program :




//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();
    }
}

Output :



1
1
true
GeeksforGeeks
65
Geek
2.3
GeeksforGeeks
2.42
234342347908
GeeksforGeeks
2
ABC

Program 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();
  
    }
}

Output :

25
14

Article Tags :