0% found this document useful (0 votes)
58 views7 pages

JAVA

This document discusses byte stream classes in Java. It provides an example of using FileInputStream and FileOutputStream to copy bytes from one file to another. It also discusses character streams, standard input/output/error streams, and reading and writing files using FileInputStream and FileOutputStream. Key methods of FileInputStream and FileOutputStream are listed. The document concludes by discussing wrapping streams and using DataOutputStream to output primitive data types to a file.

Uploaded by

kavas26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views7 pages

JAVA

This document discusses byte stream classes in Java. It provides an example of using FileInputStream and FileOutputStream to copy bytes from one file to another. It also discusses character streams, standard input/output/error streams, and reading and writing files using FileInputStream and FileOutputStream. Key methods of FileInputStream and FileOutputStream are listed. The document concludes by discussing wrapping streams and using DataOutputStream to output primitive data types to a file.

Uploaded by

kavas26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSIGNMENT-I

NAME

: SARANYA.S

ROLL NO : 1326KB39
CLASS

: II B.SC [CT] B

SUBJECT : JAVA PROGRAMMING


DATE

: 30.12.14

TOPIC

: BYTE STREAM CLASSES

FACULTY NAME : MRS.V.SATHYAVATHY

SIGNATURE

BYTE STREAM CLASSES


INTRODUCTION:
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();
}
}
}
}
CopyBytes spends most of its time in a simple loop that reads the input stream and writes the
output stream, one byte at a time, as shown in the following figure.

CHARACTER STREAM:
Java Byte streams are used to perform input and output of 8-bit bytes, where as
Java Characterstreams 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 major difference is that FileReader reads two
bytes at a time and FileWriter writes two bytes at a time.

STANDARD STREAMS:
All Standard Input: This is used to feed the data to user's program and usually a keyboard is
used as standard input stream and represented as System.in.

Standard Output: This is used to output the data produced by the user's program and
usually a computer screen is used to standard output stream and represented as System.out.

Standard Error: This is used to output the error data produced by the user's program
and usually a computer screen is used to standard error stream and represented
as System.err.
Following is a simple program which creates InputStreamReader to read standard input
stream until the user types a "q":
import java.io.*;
public class ReadConsole {
public static void main(String args[]) throws IOException
{
InputStreamReader cin = null;
try {
cin = new InputStreamReader(System.in);
System.out.println("Enter characters, 'q' to quit.");
char c;
do {
c = (char) cin.read();
System.out.print(c);
} while(c != 'q');
}finally {
if (cin != null) {
cin.close();
}

}
}
}

READING AND WRITING FILES:


As described earlier, A stream can be defined as a sequence of data. The InputStream is
used to read data from a source and the OutputStream is used for writing data to a
destination.
Here is a hierarchy of classes to deal with Input and Output streams.

The two important streams are FileInputStream and FileOutputStream, which would be
discussed in this tutorial:

FILE INPUT STREAM:


InputStream f = new FileInputStream("C:/java/hello");
Following constructor takes a file object to create an input stream object to read the file. First
we create a file object using File() method as follows:
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);

.
S
N

Methods with Description


public

void

close()

throws

IOException{}

This method closes the file output stream. Releases any system resources
associated with the file. Throws an IOException.
protected

void

finalize()throws

IOException

{}

This method cleans up the connection to the file. Ensures that the close method
of this file output stream is called when there are no more references to this
stream. Throws an IOException.
public

int

read(int

r)throws

IOException{}

This method reads the specified byte of data from the InputStream. Returns an
int. Returns the next byte of data and -1 will be returned if it's end of file.
public

int

read(byte[]

r)

throws

IOException{}

This method reads r.length bytes from the input stream into an array. Returns the
total number of bytes read. If end of file -1 will be returned.
public

int

available()

throws

IOException{}

Gives the number of bytes that can be read from this file input stream. Returns
an int.

FILE OUTPUT STREAM:


FileOutputStream is used to create a file and write data into it. The stream would create a file,
if it doesn't already exist, before opening it for output.
OutputStream f = new FileOutputStream("C:/java/hello")
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
.
S
N
1

Methods with Description


public

void

close()

throws

IOException{}

This method closes the file output stream. Releases any system resources

associated with the file. Throws an IOException.


protected
2

void

finalize()throws

IOException

{}

This method cleans up the connection to the file. Ensures that the close method
of this file output stream is called when there are no more references to this
stream. Throws an IOException.

3
4

public

void

write(int

w)throws

IOException{}

This methods writes the specified byte to the output stream.


public

void

write(byte[]

w)

Writes w.length bytes from the mentioned byte array to the OutputStream.

CONCLUSION:
If you look at the byte output stream hierarchy, the FilterOutputStream class is a
direct subclass of

the OutputStreamclass

that

implements

all

its

methods.

The FilterOutputStream class is the superclass of all classes that filter output streams. These
substreams sit on top of existing output streams such as the FileOutputStream and use the
stream as a basic data sink, possibly modifying the data along the way or providing enhanced
functionality. Using multiple streams in combination is known as wrapping as essentially
we wrap one

stream

within

another.We

will

be

using

thejava.io.DataOutputStream subclass in our example below which takes an instance


of FileOutputStream in its instantiation. This class allows us to output primitive data types to
an output stream.

REFERENCES:
docs.oracle.com/javase/tutorial/essential/io/bytestreams.html
java5tutor.info/java/apicontents/bytestreams.html
www.slideshare.net/myrajendra/byte-stream-classes49

You might also like