Unitucerjava
Unitucerjava
Java 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 the classes required
for input and output operations.
We can perform file handling in Java by Java I/O API.
Java brings various Streams with its I/O package that helps the user to perform all the input-output operations.
These streams support all the types of objects, data-types, characters, files etc to fully execute the I/O operations.
Stream
A stream can be defined as a sequence of data. In Java, a stream is composed of bytes. It's called a stream because
it is like a stream of water that continues to flow. There are two kinds of Streams −
•InPutStream − The InputStream is used to read data from a source.
•OutPutStream − The OutputStream is used for writing data to a destination.
Types of Streams:
•Depending on the type of operations, streams can be divided into two primary classes:
• Input Stream: These streams are used to read data that must be taken as an input from a source array or file
or any peripheral device. For eg., FileInputStream, BufferedInputStream, ByteArrayInputStream etc.
• Output Stream: These streams are used to write data as outputs into an array or file or monitor or any output
peripheral device. For eg., FileOutputStream, BufferedOutputStream, ByteArrayOutputStream etc.
Depending on the types of file(the data a stream holds), Streams can be divided into two primary classes:
ByteStream,CharacterStream.
ByteStream: This is used to process data byte by byte (8 bits). Though it has many classes, the FileInputStream
and the FileOutputStream are the most popular ones. The FileInputStream is used to read from the source and
FileOutputStream is used to write to the destination.
Byte stream is used to read and write a single byte (8 bits) of data.
All byte stream classes are derived from base abstract classes called InputStream and OutputStream.
Here is the list of various ByteStream Classes:
CharacterStream: In Java, characters are stored using Unicode conventions . Character stream automatically allows
us to read/write data character by character. Though it has many classes, the FileReader and the FileWriter are the
most popular ones. FileReader and FileWriter are character streams used to read from the source and write to the
destination respectively.
Character stream is used to read and write a single character of data.
All the character stream classes are derived from base abstract classes Reader and Writer.
Here is the list of various CharacterStream Classes:
FileInputStream
This class reads the data from a specific file (byte by byte). It is usually used to read the contents of a file with raw
bytes, such as images.
•Then read the contents of the specified file using either of the variants of read() method −
• int read() − This simply reads data from the current InputStream and returns the read data byte by byte (in
integer format).
• This method returns -1 if the end of the file is reached.
• int read(byte[] b) − This method accepts a byte array as parameter and reads the contents of the current
InputStream, to the given array
• This method returns an integer representing the total number of bytes or, -1 if the end of the file is reached.
• int read(byte[] b, int off, int len) − This method accepts a byte array, its offset (int) and, its length (int) as
parameters and reads the contents of the current InputStream, to the given array.
• This method returns an integer representing the total number of bytes or, -1 if the end of the file is reached.
read() Method:
•read() - reads a single byte from the file
•read(byte[] array) - reads the bytes from the file and stores in the specified array
•read(byte[] array, int start, int length) - reads the number of bytes equal to length from the file and stores in the
specified array starting from the position start.
close() Method
To close the file input stream, we can use the close() method.
Once the close() method is called, we cannot use the input stream
to read data.
available() Method
To get the number of available bytes, we can use the available() method.
skip() Method
To discard and skip the specified number of bytes, we can use the skip() method.
FileOutputStream class
FileReader
FileReader is useful to read data in the form of characters from a ‘text’ file.
•This class inherit from the Reader Class.
•FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a
FileInputStream.
Constructors:
•FileReader(File file) – Creates a FileReader , given the File to read from
•FileReader(String fileName) – Creates a new FileReader , given the name of the file to read from
Methods:
•public int read () throws IOException – Reads a single character. This method will block until a character is
available, an I/O error occurs, or the end of the stream is reached.
•public int read(char[] cbuff) throws IOException – Reads characters into an array. This method will block until
some input is available, an I/O error occurs, or the end of the stream is reached.
•public abstract int read(char[] buff, int off, int len) throws IOException –Reads characters into a portion of an
array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.
Parameters:
cbuf – Destination buffer
off – Offset at which to start storing characters
len – Maximum number of characters to read
•public void close() throws IOException closes the reader.
•public long skip(long n) throws IOException –Skips characters. This method will block until some characters are
available, an I/O error occurs, or the end of the stream is reached.
Parameters:
n – The number of characters to skip
FileWriter
FileWriter is useful to create a file writing characters into it.
•This class inherits from the Writer class.
•FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a
FileOutputStream.
•FileWriter creates the output file, if it is not present already.
Java DataInputStream Class
Java DataInputStream class allows an application to read primitive data from the input stream in a machine-
independent way.
class DataOutputStreamDemo
{
public static void main(String args[]) throws IOException
{
//writing the data using DataOutputStream
try ( DataOutputStream dout =
new DataOutputStream(new FileOutputStream("file.dat")) )
{
dout.writeDouble(1.1);
dout.writeInt(55);
dout.writeBoolean(true);
dout.writeChar('4');
}
catch(FileNotFoundException ex)
{
System.out.println("Cannot Open the Output File");
return;
}
The File class from the java.io package, allows us to work with files.
To use the File class, create an object of the class, and specify the filename or directory name