File Input Output
ETL LABS PVT LTD – JAVA PROGRAMMING 142
Input and Output
I/O (Input and Output) is used to process the
input and produce the output.
Java uses the concept of a stream to make I/O
operation fast. The java.io package contains all 3
the classes required for input and output
operations.
We can perform file handling in Java by Java
I/O API.
ETL LABS PVT LTD – JAVA PROGRAMMING 143
OutputStream class
OutputStream class is an
Method Description abstract class. It is the superclass
of all classes representing an
public void write(int)throws is used to write a byte to the output stream of bytes. An
IOException current output stream. output stream accepts output
bytes and sends them to some
sink.
public void write(byte[])throws is used to write an array of byte
IOException to the current output stream.
flushes the current output
public void flush()throws IOException
stream.
is used to close the current
public void close()throws IOException
output stream.
ETL LABS PVT LTD – JAVA PROGRAMMING 144
OutputStream class
ETL LABS PVT LTD – JAVA PROGRAMMING 145
FileOutputStream Class
Java FileOutputStream is an output stream
used for writing data to a file.
If you have to write primitive values into a file, 3
use FileOutputStream class. You can write
byte-oriented as well as character-oriented
data through FileOutputStream class. But, for
character-oriented data, it is preferred to use
FileWriter than FileOutputStream.
ETL LABS PVT LTD – JAVA PROGRAMMING 146
FileOutputStream Example
Method Description
public void write(int)throws is used to write a byte to the
IOException current output stream.
public void write(byte[])throws is used to write an array of byte
IOException to the current output stream.
flushes the current output
public void flush()throws IOException
stream.
is used to close the current
public void close()throws IOException
output stream.
ETL LABS PVT LTD – JAVA PROGRAMMING 147
FileOutputStream class methods
Method Description
protected void finalize() It is used to clean up the connection with the file output stream.
void write(byte[] ary) It is used to write ary.length bytes from the byte array to the file output stream.
void write(byte[] ary, int It is used to write len bytes from the byte array starting at offset off to the file output
off, int len) stream.
void write(int b) It is used to write the specified byte to the file output stream.
FileChannel getChannel() It is used to return the file channel object associated with the file output stream.
FileDescriptor getFD() It is used to return the file descriptor associated with the stream.
void close() It is used to closes the file output stream.
ETL LABS PVT LTD – JAVA PROGRAMMING 148
InputStream class
InputStream class is an abstract
Method Description class. It is the superclass of all
classes representing an input
public abstract int
reads the next byte of data from the stream of bytes.
input stream. It returns -1 at the end of
read()throws IOException
the file.
returns an estimate of the number of
public int available()throws
bytes that can be read from the current
IOException
input stream.
public void close()throws
is used to close the current input stream.
IOException
ETL LABS PVT LTD – JAVA PROGRAMMING 149
InputStream class
ETL LABS PVT LTD – JAVA PROGRAMMING 150
FileInputStream Class
Java FileInputStream class obtains input bytes
from a file. It is used for reading byte-oriented
data (streams of raw bytes) such as image 3
data, audio, video etc. You can also read
character-stream data. But, for reading
streams of characters, it is recommended to
use FileReader class.
ETL LABS PVT LTD – JAVA PROGRAMMING 151
FileInputStream Example
Method Description
public void write(int)throws is used to write a byte to the
IOException current output stream.
public void write(byte[])throws is used to write an array of byte
IOException to the current output stream.
flushes the current output
public void flush()throws IOException
stream.
is used to close the current
public void close()throws IOException
output stream.
ETL LABS PVT LTD – JAVA PROGRAMMING 152
FileInputStream class methods
Method Description
It is used to return the estimated number of bytes that can be read from the input
int available()
stream.
int read() It is used to read the byte of data from the input stream.
int read(byte[] b) It is used to read up to b.length bytes of data from the input stream.
int read(byte[] b, int off, int len) It is used to read up to len bytes of data from the input stream.
long skip(long x) It is used to skip over and discards x bytes of data from the input stream.
It is used to return the unique FileChannel object associated with the
FileChannel getChannel()
file input stream.
FileDescriptor getFD() It is used to return the FileDescriptor object.
It is used to ensure that the close method is call when there is no more reference to
protected void finalize()
the file input stream.
void close() It is used to closes the stream.
ETL LABS PVT LTD – JAVA PROGRAMMING 153