Difference between Byte and Character Stream
What is Byte Stream?
Byte stream is a class of a sequence of bytes that is used to perform input stream and output stream of bytes. In other words, with the help of a byte stream, we can write and read the data of the bytes.
If we need to read the data, we will use the input stream and when we need to write, we will use the output stream.
There are some phases of the Byte stream in Java:
- Split: In this phase, we split the source data into a stream with the help of a spliterator. It is an interface that breaks the stream into smaller parts so we can traverse the data easily.
- Apply: In this phase, all the elements in the stream are processed.
- Combine: In this phase, when we processed all the elements, they are combined again together to create a stream.
Types of Byte Stream
The byte stream is further divided into two parts:
- Input stream class
- Output stream class
Input Stream Class
Input stream classes are used to read the data from the source file. It has a constructor that we can use to create the instance of the input stream classes.
In other words, the input stream has a method that helps us to read the byte from the file or memory.
The input stream class has an abstract class that cannot be instantiated. There are various methods and sub-classes in the input stream classes.
Syntax of the input stream class:
FileInputStream sourceStream = new FileInputStream("path_to_file");
Sub-classes are as follows:
- Buffered input stream: It helps us to provide the methods so we can read the bytes from the buffer.
- Byte ArrayInput stream: It helps us to provide the methods so we can read the bytes from the byte array.
- Data Input stream: It helps us to provide the methods so we can read the primitive data type of Java.
- File input stream: It helps us to provide the methods so we can read the bytes from the files.
- Filter input stream: This class contains the methods to read the bytes from the other stream which can be used as the primary source of data.
- Object input stream: It helps us to provide the methods so we can read the objects.
- Piped input stream: It helps us to provide the methods so we can read them from a piped object which must be connected with the piped input stream.
- Sequence input stream: It helps us to provide the methods so we can be connected to multiple streams and read the data from all the input streams that are connected.
Different methods of the input stream class
There are various methods to read the data from the input stream:
- int read(): This method returns the integers which is used to represent the next available byte of the input stream.
- int read(byte buffer[]): It is used to read the specific lengths of the buffer bytes from the input stream and returns the total number of bytes that are successfully read.
- int available(): In this type of method, we return the number of bytes that are available to read.
- void mark(int n bytes): In this type of method, we mark the current position of the input stream until we read the desired n bytes.
- void reset(): In this type of method, we reset the previous mark point that is used to point the input stream.
Output Stream Class
Output stream class is used to write the data to the destination. It has a constructor that we can use to create the instance of the output stream classes.
In other words, it is an abstract class that is used to write the bytes to the streams. We can say that it is a superclass of all the output stream classes.
There are various methods and sub-classes in the output stream classes.
Syntax of the output stream class:
FileOutputStream target Stream = new FileOutputStream("path_to_file");
Sub-classes are as follows:
- Buffered output stream: It helps us to provide the methods so we can write the bytes to the buffer.
- Byte ArrayOutput stream: It helps us to provide the methods so we can write the bytes to the byte array.
- Data Output stream: It helps us to provide the methods so we can write the primitive data types of java.
- File Output stream: It helps us to provide the methods so we can write the bytes to the files.
- Filter output stream: This class contains the methods to write the bytes from the other stream.
- Object output stream: It helps us to provide the methods so we can write the objects.
- Piped Output stream: It helps us to provide the methods so we can write the piped output stream.
- Print stream: It helps us to provide the method so we can print the primitive data types of Java.
Different methods of the output stream class
There are various methods to read the data from the output stream:
- voidwrite(): In this method, it is used to write the specific byte to the output stream.
- Void write(byte buffer[]): In this method, it is used to write a byte array to the output stream.
- void flush(): This method is used to flush the output stream and after that, we write the pending buffered bytes.
- Void close(): This method is used to close the output stream.
Example of the byte stream
ByteArrayInputStream to read data
importjava.io.ByteArrayInputStream;
public class Main {
public static void main(String[] args) {
// Creates an array of byte
byte[] array = {1, 2, 3, 4};
try {
ByteArrayInputStream input = new ByteArrayInputStream(array);
System.out.print("The bytes read from the input stream: ");
for(inti= 0; i<array.length; i++) {
// Reads the bytes
int data = input.read();
System.out.print(data + ", ");
}
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output:
What is a Character stream?
Character stream is used to store the character value with the use of Unicode conventions. It is used to perform the input and output operation on the 16 bits Unicode.
It is used to overcome the limitations of the byte stream classes. In other words, it is used to read the data from the source file and write the data to the destination.
We can also use a character stream to copy a character text file from one source to another destination using the stream.
There are some phases of the character stream in Java:
- Split: In this phase, we split the source data into a stream with the help of a spliterator. It is an interface that breaks the stream into smaller parts so we can traverse the data easily.
- Apply: In this phase, all the elements in the stream are processed.
- Combine: In this phase, when we processed all the elements, they are combined again together to create a stream.
Types of character stream
The character stream is further divided into two parts:
- Reader stream class
- Writer stream class
Reader stream class
Reader stream class or file reader is used to reading two bytes at a time from the source file. It has a constructor that we can use to create the instance of the reader stream classes.
The reader stream class has an abstract class that cannot be instantiated. But there are various methods and sub-classes in the reader stream classes that help us to override the method of the reader classes.
Syntax of the reader stream class:
FileReader in = new FileReader("path_to_file");
Sub-classes are as follows:
- Buffered Reader: It helps us to provide the methods so we can read the character from the buffer.
- Char Array reader: It helps us to provide the methods so we can read the character from the character array.
- File Reader: It helps us to provide the methods so we can read the character from the files.
- Filter Reader: This class contains the methods to read the character from the character input stream that is underlying.
- Input stream reader: It helps us to provide the methods so we can easily convert any byte into a character.
- Piped input stream: It helps us to provide the methods so we can read characters from a piped object which must be connected with the piped output stream.
- String reader: It helps us to provide the methods so we can read the character from the string.
Different methods of the Reader class
There are various methods to read the data from the reader stream:
- int read(): This method returns the integers which is used to represent the next available character of the input stream.
- int read(char buffer[]): It is used to read the specific buffer character from the reader stream and returns the total number of character that is successfully read.
- void mark(int n chars): In this type of method, we mark the current position of the input stream until we read the desired n chars.
- void reset(): In this type of method, we reset the previous mark point that is used to point the input stream.
- Void close(): It is used to close the input stream and if the program tries to access the input it will automatically generate the IO exception.
- Boolean ready(): In this type of method, we return the true value of Boolean if the next input is ready.
Writer stream class
Writer stream class or file writer is used to write two bytes at a time to the destination file. It has a constructor that we can use to create the instance of the writer stream classes.
The writer stream class has an abstract class that cannot be instantiated. But there are various methods and sub-classes in the writer stream classes that help us to write the character on the output stream.
Syntax of the writer stream class:
FileWriter in = new FileWriter("path_to_file");
Sub-classes are as follows:
- Buffered writer: It helps us to provide the methods so we can write the character to the buffer.
- Char Array writer: It helps us to provide the methods so we can write the character to the character array.
- File Reader: It helps us to provide the methods so we can write the character from the files.
- Output stream writer: It helps us to provide the methods so we can easily convert any byte into a character.
- Piped input stream: It helps us to provide the methods so we can write a character to the piped output stream.
- String writer: it helps us to provide the methods so we can write the character to the string.
Different methods of the writer class
There are various methods to read the data from the writer stream:
- void write(): In this method, we can write the data to the output stream.
- Void write(inti): In this method, we can only write a single character to the output stream.
- Void write(char buffer[]): In this type of method, we can write the array of characters to the output stream.
- Void close(): It is used to close the input stream and if the program tries to write to the output it will automatically generate the IO exception.
- Void flush(): It is used to flush the output stream and write the waiting buffer characters.
Difference between Byte and Character Stream
Byte Stream | Character Stream |
In the byte stream, all the Input Stream/Output Stream class is byte-oriented. | In the character stream, all the input and output streams are character-oriented. |
In the Byte stream, we access the data file byte by byte (8 bits). | In the character stream, we can access the data file character by character. |
Byte stream ends with the suffix input or output stream. | Character stream end with the suffix reader or writer. |
Byte stream classes are classified into : (i) Input Stream classes, (ii) Output Stream Classes | Character stream classes are classified into (i) Reader Class and (ii) Writer Class |
In byte-stream, we can translate the character stream into byte stream with the help of an output stream writer. | In the character stream, we can translate the byte stream into the character stream with the help of an input stream reader. |
It generally works in the byte parameters. | Character streams generally accept parameters of data type char parameters. |
Byte streams are generally used for the read and write in the byte form. | Advantage of character streams that is they make it easy to write programs that are not dependent upon a specific character encoding. |