What Are The Different Types Of Stream API In Java?

Asked 8 months ago
Answer 1
Viewed 276
1

Java 8 allows you to create streams out of three primitive types: int, long, and double. Because Stream is a generic interface, primitives cannot be used as type parameters. Therefore, three new special interfaces were created: IntStream, LongStream, and DoubleStream

Java gives I/O Streams to peruse and compose information where, a Stream addresses an info source or a result objective which could be a record, I/o devise, other program and so on.

As a general rule, a Stream will be an info stream or, a result stream.

  • InputStream − This is used to read data from a source.
  • OutputStream − This is used to write data to a destination.

Based on the data they handle there are two types of streams 

  • Byte Streams − These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, audios, images etc.
  • Character Streams − These handle data in 16 bit Unicode. Using these you can read and write text data only.

Standard Streams

In addition to above mentioned classes Java provides 3 standard streams representing the input and, output devices.

  • Standard Input − This is used to read data from the user's input devices. The keyboard serves as the standard input stream and is represented as System.in.
  • Standard Output − This is used to present data (results) to the user via output devices. A computer screen is used for the standard output stream, which is represented as System.out.
  • Standard Error − This is used to output the error data generated by the user's program, and a computer screen is typically used for the standard error stream, which is represented as System.err.

Example

The following Java software reads data from the user using BufferedInputStream and sends it to a file using BufferedOutputStream.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedInputStreamExample {
   public static void main(String args[]) throws IOException {
      //Creating an BufferedInputStream object
      BufferedInputStream inputStream = new BufferedInputStream(System.in);
      byte bytes[] = new byte[1024];
      System.out.println("Enter your data ");
      //Reading data from key-board
      inputStream.read(bytes);
      //Creating BufferedOutputStream object
      FileOutputStream out= new FileOutputStream("D:/myFile.txt");
      BufferedOutputStream outputStream = new BufferedOutputStream(out);
      //Writing data to the file
      outputStream.write(bytes);
      outputStream.flush();
      System.out.println("Data successfully written in the specified file");
   }
}

Output

Enter your data
Hi welcome to YourQuorum....
Data successfully written in the specified file
Read Also : Who was the only American to play James Bond?
Answered 8 months ago Pirkko  KoskitaloPirkko Koskitalo