Java Io Basics
Java Io Basics
IOException :
Streams :
ByteStream:
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.
Java InputStream class is the superclass of all the io classes i.e. representing
an input stream of bytes.
Applications that are defining a subclass of the Java InputStream class must
provide a method, that returns the next byte of input.
Output Stream:
These streams are used to write data as outputs into an array or file or any
output peripheral device. For eg., FileOutputStream, BufferedOutputStream,
ByteArrayOutputStream etc.
An output stream accepts output bytes and sends them to some sink .
Constructor and Description
OutputStream() : Single Constructor
Reader
Writer
Reader Class
Reader class is used to read the 16-bit characters from the input stream.
it is an abstract class and can't be instantiated, but there are various subclasses
that inherit the Reader class and override the methods of the Reader class.
All methods of the Reader class throw an IOException.
Class Description
1. BufferedReader This class provides methods to read characters from the buffer.
2. CharArrayReader This class provides methods to read characters from the char array.
3. FileReader This class provides methods to read characters from the file.
4. FilterReader This class provides methods to read characters from the underlying character
input stream.
Writer class
Writer class is used to write 16-bit Unicode characters to the output stream.
Like Reader class, Writer class is also an abstract class that cannot be
instantiated.
the subclasses of the Writer class are used to write the characters onto the
output stream.
SN Class Description
3 CharArrayWriter This class provides methods to write the characters to the character
array.
5 PipedWriter This class provides methods to write the characters to the piped output
stream.
6 StringWriter This class provides methods to write the characters to the string.
Example Of Character Stream
1. java.io package
2. java.nio package
The java.io package is used for file handling in Java. It provides various classes for
handling files, such as File, FileReader, FileWriter, FileInputStream, and
FileOutputStream.
The java.nio package is the newer file handling library, introduced in Java 1.4. It
provides better performance and scalability than the java.io package. It provides
classes like FileChannel, ByteBuffer, and Charset.
To read data from a file, you need to follow the following steps:
Create a File object that represents the file you want to read.
Use the read() method of the FileReader object to read data from the file.
1. Create a File object that represents the file you want to write to.
3. Use the write() method of the FileWriter object to write data to the file.
FileOutputStream