Data and Object Streams

Data and Object Streams

Data Streams and Object Streams are two types of Java I/O streams that allow Java programs to read and write data and objects to and from files or other streams.

Data Streams:

DataStreams are used to read or write primitive data types such as int, float, double, long, boolean, etc. Data streams read and write data in its binary format, which means that they are not human-readable.

To use DataStreams, you first create an instance of the DataInputStream or DataOutputStream class, and then use its read and write methods to read and write primitive data types.

Example code to read and write data to a file using DataInputStream and DataOutputStream:

import java.io.*;

public class DataStreamExample {

   public static void main(String[] args) throws IOException {

      FileOutputStream fos = new FileOutputStream(“data.bin”);

      DataOutputStream dos = new DataOutputStream(fos);

      dos.writeBoolean(true);

      dos.writeInt(12345);

      dos.writeDouble(3.14);

      dos.writeUTF(“Hello, world!”);

      FileInputStream fis = new FileInputStream(“data.bin”);

      DataInputStream dis = new DataInputStream(fis);

      boolean b = dis.readBoolean();

      int i = dis.readInt();

      double d = dis.readDouble();

      String s = dis.readUTF();

      System.out.println(b);

      System.out.println(i);

      System.out.println(d);

      System.out.println(s);

      dos.close();

      dis.close();

   }

}

Object Streams:

ObjectStreams are used to read and write objects to and from files or other streams. ObjectStreams read and write objects in a serialized format, which means that the objects are converted to a stream of bytes that can be written to a file or sent over a network.

To use ObjectStreams, you first create an instance of the ObjectOutputStream or ObjectInputStream class, and then use its writeObject and readObject methods to write and read objects.

Example code to read and write objects to a file using ObjectOutputStream and ObjectInputStream:

import java.io.*;

public class ObjectStreamExample {

   public static void main(String[] args) throws IOException, ClassNotFoundException {

      FileOutputStream fos = new FileOutputStream(“object.bin”);

      ObjectOutputStream oos = new ObjectOutputStream(fos);

      Person p = new Person(“John”, 25);

      oos.writeObject(p);

      FileInputStream fis = new FileInputStream(“object.bin”);

      ObjectInputStream ois = new ObjectInputStream(fis);

      Person p2 = (Person)ois.readObject();

      System.out.println(p2.getName());

      System.out.println(p2.getAge());

      oos.close();

      ois.close();

   }

}

class Person implements Serializable {

   private String name;

   private int age;

   public Person(String name, int age) {

      this.name = name;

      this.age = age;

   }

   public String getName() {

      return name;

   }

   public int getAge() {

      return age;

   } }

Apply for Core Java Developer Certification Now!!

https://www.vskills.in/certification/certified-core-java-developer

Back to Tutorial

Share this post
[social_warfare]
Command Line I/O
Java.lang

Get industry recognized certification – Contact us

keyboard_arrow_up