What is a Stream and what are the types of Streams and classes in Java?

 In Java, a stream is a sequence of data elements that can be processed sequentially. It represents a flow of data from a source to a destination, where the data can be read from or written to. Streams provide a convenient and efficient way to handle input and output operations in Java.


Java provides two types of streams:


1. Byte Streams: Byte streams are used to handle input and output of raw bytes. They are represented by classes that end with `InputStream` or `OutputStream`. Some commonly used byte stream classes are:

   - `InputStream`: This abstract class serves as the superclass for all byte input streams. Examples include `FileInputStream`, `ByteArrayInputStream`, and `BufferedInputStream`.

   - `OutputStream`: This abstract class serves as the superclass for all byte output streams. Examples include `FileOutputStream`, `ByteArrayOutputStream`, and `BufferedOutputStream`.


2. Character Streams: Character streams are used to handle input and output of characters. They are represented by classes that end with `Reader` or `Writer`. Character streams automatically handle character encoding and decoding. Some commonly used character stream classes are:

   - `Reader`: This abstract class serves as the superclass for all character input streams. Examples include `FileReader`, `StringReader`, and `BufferedReader`.

   - `Writer`: This abstract class serves as the superclass for all character output streams. Examples include `FileWriter`, `StringWriter`, and `BufferedWriter`.





Example

Certainly! Here's an example that demonstrates the usage of byte streams and character streams in Java:


1. Byte Streams Example:

```java

import java.io.*;


public class ByteStreamsExample {

    public static void main(String[] args) {

        try {

            FileInputStream fis = new FileInputStream("input.txt");

            FileOutputStream fos = new FileOutputStream("output.txt");


            int byteData;

            while ((byteData = fis.read()) != -1) {

                fos.write(byteData);

            }


            fis.close();

            fos.close();


            System.out.println("File copied successfully using byte streams.");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

```

In this example, we use `FileInputStream` to read bytes from an input file (`input.txt`) and `FileOutputStream` to write those bytes to an output file (`output.txt`), essentially copying the contents of one file to another.


2. Character Streams Example:

```java

import java.io.*;


public class CharacterStreamsExample {

    public static void main(String[] args) {

        try {

            FileReader reader = new FileReader("input.txt");

            FileWriter writer = new FileWriter("output.txt");


            int charData;

            while ((charData = reader.read()) != -1) {

                writer.write(charData);

            }


            reader.close();

            writer.close();


            System.out.println("File copied successfully using character streams.");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

```

In this example, we use `FileReader` to read characters from an input file (`input.txt`) and `FileWriter` to write those characters to an output file (`output.txt`), performing a similar task as the previous example but using character streams.


Both examples read data from the source file and write it to the destination file byte by byte or character by character until the end of the file is reached. Finally, the streams are closed to release system resources, and a success message is printed.


"A true friend is someone who is always there during the ups and downs, and accepts you for who you are




Post a Comment

Previous Next

نموذج الاتصال