QA's approach 2 Java - File Handling Theory

Java IO is an API that comes with Java which is targeted at reading and writing data (input and output). Most applications need to process some input and produce some output based on that input. For instance, read data from a file or over network, and write to a file or write a response back over the network.

The Java IO package is primarily focused on input and output to files, network streams, internal memory buffers etc. However, the Java IO package does not contain classes to open network sockets which are necessary for network communication. For that purpose you need to use the Java Networking API. Once you have opened a socket (network connection) though, you read and write data to and from it via Java IO's InputStream and OutputStream classes.

Basic Terminologies:

Stream: A stream is a sequence of data. 

Streams in Java IO can be either byte based (reading and writing bytes) or character based (reading and writing characters).
Java defines two types of streams:

1. Byte streams: Byte streams should only be used for the most primitive I/O.

Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream. File I/O byte streams ->FileInputStream and FileOutputStream.

Input Stream:\A program uses an input stream to read data from a source, one item at a time:

Using InputStream object we always represent address of the resource from where that has to be read or recive the data from any location to the java program

Output Stream: A program uses an output stream to write data to a destination, one item at time:

Using object of the OutputStream class we represent address of the destination,To which the data has to be sent.Without mentioning address of the destination the data cannot be transferred or send.

2. Character Stream: It provides a convenient means for handling input and output of characters.

We will see streams that can handle all kinds of data, from primitive values to advanced objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.

Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Data Source & Destination can be a Disk, Network socket, Peripheral device, an array or another program.

Name of all the Byte classes ends withStream.

Name of all the Character classes ends with Reader/Writer.

Abstract classes:
An InputStream or Reader is linked to a source of data. An OutputStream or Writer is linked to a destination of data.

                         Source -->InputStream/Reader-->Program

                         Program -->OutputStream/Writer-->Destination

Java IO contains many subclasses of the InputStream, OutputStream, Reader and Writerclasses.

Some important concrete classes of InputStream & outputStream abstract classes of Byte Stream

Stream class Description
BufferedInputStream Used for Buffered Input Stream.
BufferedOutputStream Used for Buffered Output Stream.
DataInputStream Contains method for reading java standard datatype
DataOutputStream An output stream that contain method for writing java standard data type
FileInputStream Input stream that reads from a file
FileOutputStream Output stream that write to a file.
InputStream Abstract class that describe stream input.
OutputStream Abstract class that describe stream output.
PrintStream Output Stream that contain print() and println() method

Key methods:
  1. read() : reads byte of data.
  2. write() : Writes byte of data
Some important concrete classes of Reader & Writer abstract classes of Character stream

Stream class Description
BufferedReader Handles buffered input stream.
BufferedWriter Handles buffered output stream.
FileReader Input stream that reads from file.
FileWriter Output stream that writes to file.
InputStreamReader Input stream that translate byte to character
OutputStreamReader Output stream that translate character to byte.
PrintWriter Output Stream that contain print() and println() method.
Reader Abstract class that define character stream input
Writer Abstract class that define character stream output

How do we take inputs from Keyboard?

Buffered reader object is used to take inputs from Keyboard.


Reading Characters:
read() method is used with BufferedReader object to read characters. As this function returns integer type value has we need to use typecasting to convert it into char type.

int read()

Reading Strings:
To read string we have to use readLine() function with BufferedReader class's object.

String readLine()

Java.nio package

The New Input/Ouput (NIO) solves the slow speed problem in the stream-based I/O.

In NIO, we deal with channels and buffers for I/O operations.

A channel is like a stream. It represents a connection between a data source and a Java program.

A channel provides a two-way data transfer facility. We can use a channel to read data as well as to write data.

We can obtain a read-only channel, a write-only channel, or a read-write channel.

A buffer is a bounded data container and has a fixed capacity that determines the upper limit of the data it may contain.

In stream-based I/O, we write data directly to the stream. 

In channel-based I/O, we write data into a buffer and we pass that buffer to the channel, which writes the data to the data destination.
When reading data from a data source, we pass a buffer to a channel. The channel reads data from the data source into a buffer.


Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?