What is meant by stream? Explain byte stream and character stream classes with example
Stream
A stream is a way of sequentially accessing a file. In Streams, you can process the data one at a time as bulk operations are unavailable with them. But, streams supports a huge range of source and destinations including disk file, arrays, other devices, other programs, etc. In Java, a byte is not the same thing as a char. Therefore a byte stream is different from a character stream. So, Java defines two types of streams: Byte Streams and Character Streams.
Byte Streams
A byte stream access the file byte by byte. Java programs use byte streams to perform input and output of 8-bit bytes. It is suitable for any kind of file, but not quite appropriate for text files. For example, if the file is using a Unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself. Byte-oriented streams do not use any encoding scheme while Character oriented streams use character encoding schemes (UNICODE). All byte stream classes are descended from InputStream and OutputStream.
Example
import java.io.*;
public class TestClass{
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("in.txt");
fos = new FileOutputStream ("out.txt");
int temp;
while ((temp = fis.read()) != -1) //read byte by byte
fos.write((byte)temp); //write byte by byte
if (fis != null)
fis.close();
if (fos != null)
fos.close();
}catch(Exception e){
System.out.println(e);
}
}
}
When to use:
Byte streams should only be used for the most primitive I/O
When not to use:
You should not use Byte stream to read Character streams e.g. To read a text file
Character Streams
A character stream will read a file character by character. Character Stream is a higher-level concept than Byte Stream. A Character Stream is, effectively, a Byte Stream that has been wrapped with logic that allows it to output characters from a specific encoding. That means a character stream needs to be given the file's encoding in order to work properly. Character stream can support all types of character sets ASCII, Unicode, UTF-8, UTF-16 etc. All character stream classes are descended from Reader and Writer.
Example
import java.io.*;
public class TestClass{
public static void main(String[] args) {
FileReader reader = null;
try {
reader = new FileReader("in.txt");
int fChar;
while ((fChar = reader.read()) != -1) //read char by char
System.out.println((char)fChar); //write char by char
}catch(Exception e){
System.out.println(e);
}
}
}
When to use:
To read character streams either from Socket or File of characters
Stream
A stream is a method to sequentially access a file. I/O Stream means an input source or output destination representing different types of sources e.g. disk files. The java.io package provides classes that allow you to convert between Unicode character streams and byte streams of non-Unicode text.
Stream – A sequence of data.
Input Stream: reads data from the source.
Output Stream: writes data to the destination.
Character Stream
In Java, characters are stored using Unicode conventions. Character stream automatically allows us to read/write data character by character. For example, FileReader and FileWriter are character streams used to read from the source and write to the destination.In Java, characters are stored using Unicode conventions. Character stream is useful when we want to process text files. These text files can be processed character by character. The character size is typically 16 bits.
// Java Program illustrating that we can read a file in
// a human-readable format using FileReader
import java.io.*; // Accessing FileReader, FileWriter, IOException
public class GfG
{
public static void main(String[] args) throws IOException
{
FileReader sourceStream = null;
try
{
sourceStream = new FileReader("test.txt");
// Reading sourcefile and writing content to
// target file character by character.
int temp;
while ((temp = sourceStream.read()) != -1)
System.out.println((char)temp);
}
finally
{
// Closing stream as no longer in use
if (sourceStream != null)
sourceStream.close();
}
}
}
Output: Shows contents of the file test.txt
Byte Stream
Byte streams process data byte by byte (8 bits). For example, FileInputStream is used to read from the source and FileOutputStream to write to the destination. Byte oriented reads byte by byte. A byte stream is suitable for processing raw data like binary files.
// Java Program illustrating the Byte Stream to copy
// contents of one file to another file.
import java.io.*;
public class BStream
{
public static void main(String[] args) throws IOException
{
FileInputStream sourceStream = null;
FileOutputStream targetStream = null;
try
{
sourceStream = new FileInputStream("sorcefile.txt");
targetStream = new FileOutputStream ("targetfile.txt");
// Reading source file and writing content to target
// file byte by byte
int temp;
while ((temp = sourceStream.read()) != -1)
targetStream.write((byte)temp);
}
finally
{
if (sourceStream != null)
sourceStream.close();
if (targetStream != null)
targetStream.close();
}
}
}
Comments
Post a Comment