With its I/O package, Java includes a variety of Streams that make it easier for the user to carry out any input-output tasks. For complete I/O operation execution, these streams support all object, data-type, character, and file kinds.
A data stream is a collection of data. A stream in Java is made up of bytes. It resembles a flowing stream of water, which is why it is called a stream.
Before looking at different input and output streams, let’s take a closer look at the three standard or default streams that Java offers and are also the most widely used:
Under the java.io package, Stream is contained by Java. Java specifies two different stream types. It’s them,
The two abstract classes InputStream and OutputStream at the top of the hierarchy are used to define bytes streams.
An output stream is used by a Java application to write data to a destination, such as a file, an array, a peripheral device, or a socket.
A Java application utilizes an input stream when reading data from a source—a file, an array, a peripheral device, or a socket.
// Java Program illustrating the
// Byte Stream to copy
// contents of one file to another file.
import java.io.*;
public class Coderz {
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();
}
}
}
Shows contents of file test.txt
Reader and Writer are the two abstract classes at the top of the hierarchy used to define character streams.
Stream class | Description |
---|---|
BufferedReader | It is used to handle buffered input stream. |
FileReader | This is an input stream that reads from a file. |
InputStreamReader | This input stream is used to translate byte to character. |
OutputStreamReader | This output stream is used to translate character to byte. |
Reader | This is an abstract class that defines character stream input. |
PrintWriter | This contains the most used print() and println() method |
Writer | This is an abstract class that defines character stream output. |
BufferedWriter | This is used to handle the buffered output stream. |
FileWriter | This is used to output stream that writes to the file. |
import java. Io *;
// Java Program illustrating that
// we can read a file in a human-readable
// format using FileReader
class Coderz
{
public static void main(String[] args)
{
try
{
File fl = new File("d:/myfile.txt");
// Reading sourcefile and
// writing content to target file
// character by character.
BufferedReader br = new BufferedReader(new FileReader(fl)) ;
String str;
while ((str=br.readLine())!=null)
{
System.out.println(str);
}
br.close();
fl.close();
// Closing stream as no longer in use
}
catch(IOException e) {
e.printStackTrace();
}
}
}
Shows contents of file
Note: also read about the Autoboxing and Unboxing in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…