Byte and Character Streams || Java.IO || Bcis Notes

Byte and Character Streams || Java.IO || Bcis Notes

Byte and Character Streams

Byte and Character Streams are described below:-

The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, object, localized characters, etc.

Byte Streams

Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream. There are many byte stream classes. To demonstrate how byte streams work, we’ll focus on the file I/O byte streams, FileInputStream and FileOutputStream. Other kinds of byte streams are used in much the same way; they differ mainly in the way they are constructed.

Using Byte Streams

We’ll explore FileInputStream and FileOutputStream by examining an example program named CopyBytes, which uses byte streams to copy xanadu.txt, one byte at a time.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(“xanadu.txt”);
out = new FileOutputStream(“outagain.txt”);
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}

Character Stream

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. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here the major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.

Java write file

Write Text Files
The java.io.PrintWriter class provides methods to write to text files. Its constructor takes a file name as the parameter; it creates a new file, or truncate the existing file to zero sizes.
public PrintWriter(String fileName) throws FileNotFoundException

Example

import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class WriteFile
{
public static void main(String[] args)
throws FileNotFoundException
{
PrintWriter out = new PrintWriter(“random.txt”);
int line = 1;
while (line <= 100)
out.println((line++) + “: ” + Math.random());
out.close();
}
}

Java read file

The class java.io.File is an abstract representation of file and directory pathnames. Together with the java.util.Scanner class, we can read text files. The following code creates a Fileobject, and then passes it to a Scanner object.
Scanner scan = new Scanner(new File(“test.txt”));

Example

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class ReadFile
{
public static void main(String[] args)
{
Scanner scan;
try
{
scan = new Scanner(new File(“test.txt”));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
return;
}

int line = 1;
while (scan.hasNext())
{
String str = scan.nextLine();
System.out.println((line++) + “: ” + str);
}
}
}

Java Data Input and Output Stream

Data Input Stream class:
This is used for two purposes. They are reading the data from an input device like a keyboard and reading the data from a remote machine like a server.

Data Output Stream class:
This is used for displaying the data onto the console and also used for writing the data to a remote machine.

 

Difference Between Byte Stream and Character Stream in Java

Definition
Byte Stream is a mechanism that performs input and output of 8-bit bytes while Character Stream is a mechanism in Java that performs input and output operations of 16-bit Unicode. Thus, this is the main difference between Byte Stream and Character Stream in Java.

Functionality
Another difference between Byte Stream and Character Stream in Java is that Byte Stream performs input and output operations of 8-bit bytes while Character stream performs input and output operations of 16-bit Unicode.

Associated Classes
The common classes for byte streaming in Java are FileInputStream and FileOutputStream. However, the common classes for Character streaming in Java are FileReader and FileWriter. Hence, this is another difference between Byte Stream and Character Stream in Java.

Conclusion
A stream refers to a sequence of data. Two methods of performing operations on streams in Java are using Byte Stream and character stream. The main difference between Byte Stream and Character Stream in Java is that Byte Stream helps to perform input and output operations of 8-bit bytes while Character stream helps to perform input and output operations of 16-bit Unicode.

You may also like inheritance

Be the first to comment

Leave a Reply

Your email address will not be published.


*