Table of Contents
A stream is a sequence of data. I/O Stream refers to a stream that is unlikely 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. Following are the two kinds of streams you should know:
- Input Stream: reads data from the source.
- Output Stream: writes data to a destination.
Byte Streams
Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are, FileInputStream and FileOutputStream.
Learn Coding in your Language! Enroll Here!
Example of Byte Stream Classes in Java
// Java Program Illustrate ByteStream Class to
// Copy Contents of One File to Another File
// Importing required classes
import java.io.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Initially assigning null ot objects for
// reading and writing to file
FileInputStream sourceStream = null;
FileOutputStream targetStream = null;
// Try block to check for exceptions
try {
// Passing the files via lcoal directory
sourceStream = new FileInputStream(
“/Users/mayanksolanki/Desktop/demo.rtf”);
targetStream = new FileOutputStream(
“/Users/mayanksolanki/Desktop/democopy.rtf”);
// Reading source file and writing content to
// target file byte by byte
int temp;
// If there is content inside file
// than read
while ((temp = sourceStream.read()) != -1)
targetStream.write((byte)temp);
// Display message for successfu execution of program
System.out.print(“Program successfully executed”);
}
// finally block that executes for sure
// where we are closing file connections
// to avoid memory leakage
finally {
if (sourceStream != null)
sourceStream.close();
if (targetStream != null)
targetStream.close();
}
}
}
Learn to code from industry experts! Enroll here
Character Streams
1: What is the default value of a boolean in Java?
In Java, characters are stored using Unicode conventions. Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java Character streams are used to perform input and output for 16-bit Unicode. Though there are many classes related to character streams but the most frequently used classes are, FileReader and FileWriter.
Example of Character Stream Classes in Java
// Java Program illustrate Reading
// a File in Human Readable
// Format Using FileReader Class
// Importing required classes
import java.io.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Initially assiging null as we have not read
// anything
FileReader sourceStream = null;
// Try block to check for exceptions
try {
// Reading from file
sourceStream = new FileReader(
“/Users/mayanksolanki/Desktop/demo.rtf”);
// Reading sourcefile and writing content to
// target file character by character.
int temp;
// If there is content inside file
// than read
while ((temp = sourceStream.read()) != -1)
System.out.println((char)temp);
// Display message for successfu execution of program
System.out.print(“Program successfully executed”);
}
// finally block that executes for sure
// where we are closing file connections
// to avoid memory leakage
finally {
// Closing stream as no longer in use
if (sourceStream != null)
sourceStream.close();
}
}
}
When to use Character Stream over Byte Stream?
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. Character size is typically 16 bits.
When to use Byte Stream over Character Stream?
Byte oriented reads byte by byte. A byte stream is suitable for processing the raw data like binary files.
A Java stream acts as a file handling wrapper that operates according to the corresponding I/O constructs. In many cases, character-oriented stream classes and byte-oriented stream classes function in a very similar fashion. But it also does not mean they are the same.
Entri provides video classes as well on various important topics by the excellent faculties. One will get revision modules, monthly tests based on the classes. Entri provides an excellent platform with full-length mock tests including previous year question papers. It also gives you access to clarify your doubts.