Character Streams and Byte Streams in Java

Introduction

In  previous lesson    we studied about the streams and type of the streams in java.In this lesson we are going to discuss about the two important streams viz.Byte Streams and Character Streams.

Byte Streams and Character Streams

Byte Streams  : These are the streams used by the programs to read data Byte by Byte.These streams are descended from Input / Output Stream.Examples of  byte stream are FileInputStream and FileOutputStream.

Using Byte Streams

The following example illustrates Byte Streams by copying one text file byte by byte.

package friends;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ByteStreamExample {
    public static void main(String[] args) throws IOException {

        FileInputStream instrt = null;
        FileOutputStream outstrt = null;

        try {
            int b;
            instrt = new FileInputStream("abc.txt");
            outstrt = new FileOutputStream("out.txt");

            while ((b = instrt.read()) != -1) {
                outstrt.write(b);
            }
        } finally {
            if (instrt != null) {
                instrt.close();
            }
            if (outstrt != null) {
                outstrt.close();
            }
        }
    }
}

In the above example from an input file abc.txt ,data will be copied to out.txt file byte by byte.In the above example b reads byte by byte and b returns '-1 ' when end of stream is reached and loop ends.

Note : It is always necessary to close the streams and Byte streams should be used for the most primitive I/O only.


Character Streams  : We all aware that java uses Unicode convention to store the characters. Then there is conversion or translation is required from or to local character coding.This is done through Character Input /  Output Streams.This translation will be done automatically.Examples of  byte stream are FileReader and FileWriter.Now, we are going to repeat the above example with character streams.

package friends;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CharastrExample {
    public static void main(String[] args) throws IOException {

        FileReader instrt = null;
        FileWriter outstrt = null;

        try {
            int b;
            instrt = new FileReader("abc.txt");
            outstrt = new FileWriter("out.txt");

           while ((b = instrt.read()) != -1) {
                outstrt.write(b);
            }
        } finally {
            if (instrt != null) {
                instrt.close();
            }
            if (outstrt != null) {
                outstrt.close();
            }
        }
    }
}




In the above example from an input file abc.txt ,data will be copied to out.txt file character by character.In the above example b reads byte by byte and b returns '-1 ' when end of stream is reached and loop ends.

PREVIOUS                                                                 NEXT

0 comments:

Post a Comment