ByteStreams And CharacterStreams
A stream can represent many different kinds of sources and
destinations including disk file, device, other program and memory arrays. An I/o
stream represents an source or an output destination. Stream support many kinds of data including a simple byte, primitive
data types, localized characters, and objects. Some Streams simply pass on data,
other manipulation and transform the data in useful ways.
No matter how they work internally all
streams present the same simple model to program that use them: a stream is a
sequence of data.
Program use byte streams to perform input and output of 8-bit bytes. All byte
stream classes are descended from inputStream or outputStream. there are many
byte stream classes. To demonstrate how byte stream work, we will focus on the
file I/O byte streams, FileInputStream and FileOutputStream. Other kind of Byte
Stream are used much the way; they differ mainly in the way they are
constructed.
Example of ByteStreams
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class MyByteStream
- {
- public static void main(String arg[]) throws IOException
- {
- FileInputStream fin=null;
- FileOutputStream fout=null;
- try
- {
- fin=new FileInputStream("input.txt");
- fout=new FileOutputStream("output.txt");
- int c;
- while((c=fin.read())!=-1)
- {
- fout.write(c);
- }
- }
- finally
- {
- if(fin!=null)
- {
- fin.close();
- }
- if(fout!=null)
- {
- fout.close();
- }
- }
- }
- }
Before compilation, the output file is blank but after compilation the content of input file copy into the output.

Join the conversation! Your thoughts help the community grow.