IOStream in Java

In Java, All pre-defined classes belong to the java.io the package is called IOStream. IOStream is meant for storing the data permanently to the file, performing persistence operations on a file, reading values from the keyboard, and displaying values to the console. I => Input, O => Output, Stream => Connection

It is also called as File Handling or File IO or File IO stream. The java.io package is not only having pre-defined classes for reading and storing data to a file but also having pre-defined classes for reading data from the keyboard and displaying data to the console, so it is better to call IOStreams rather than File IO or File Handling.

In Java IOStream, we will learn how to store data permanently to the file, and how to read those permanent data from the file. How to read data from the console and display data on the console. What is happening when we use System.in, System.out and System.err in our program, internally which class objects are used?

Java IOStream is used to read/write data in a flat-file. By default, IOStreams supports only flat files. The file that doesn’t support any special format and contains only plain text is called flat files. The flat files are platform-independent and it can work in all operating systems. It can be opened by any text editor software. Flat-files can contain any extension but generally, it contains .txt extension.

Terminologies used in Java IOStream

Need of storing data permanently? The values stored in a program either in variables/objects are available temporarily, only during the program execution. Once program execution is completed the variables/objects memory destroyed and all values are lost, later we can’t get those data. Program execution is done in RAM which stores values only for a temporary period. If we want to get values/data/results, even after the program execution completed then we must store those values in a permanent place i.e. persistence store. After storing data in a persistent store, later we can perform read and write operation.

Persistence Store:- The environment that allows us to store data permanently. We can store data permanently in three places:- File, Database, Remote Computer

Persistence:- The process of storing and managing data permanently in a persistence store for a long time.

Persistence Logic:- The logic that persists data in a persistence store. Example:- IOStreams code, JDBC code, Hibernate code, Spring JDBC code, Networking code.

Persistence Operations:- Insert, update, delete, select operations are called persistence operations. These are also called as CRUD operation. C=> create, R=> read, U=> update, D=> delete.

Persistence Technologies

The technology that provides API to develop persistence logic is called persistence technology.

In Java, well known persistence technologies are,

  • IOStreams:- To persist the data in flat files.
  • JDBC, Spring JDBC, ORM (Hibernate), Spring ORM, Spring Data:- To persist data in the database
  • Networking:- To persist data in the remote computer

Persistence technologies provide API to developing persistence logic, not to develop the business logic.

When we should choose a file or database? If we want to store a small amount of data without bothering the security of those data, in that case, we can work with the file. Example:- desktop games, mobile games. In these places, they have to store the top 10 payers’ names & score, score/power given to players. These data can be easily stored in the file, and it doesn’t require security. But if we want to store a large amount of data with security then we must choose a database.

Generally, we don’t use Java IOStream to store important data to the file because the file has several problems like security issues (anyone can access the file and read the data), can’t maintain data in particular columns & rows order. By increasing the data size the file size also increases and it can create the reading problem, inside a file we don’t have a searching algorithm. Flat-files are not used to store business value rather than use databases.

Streams and its Types

Streams:- Stream is a logical connection between Java application and file/console/network through which we can transfer the data either for storing or reading.

The stream can be defined as “It is a continuous flow of data between Java program and persistence store”. Or we can say “Stream is a Java object that allows reading and writing data to a persistence store”.

Note:- We need separate streams to read input and write output.

Types of streams based on data flow,

1) Input Stream:- The stream through which we can read data from persistence store to Java application.

2) Output Stream:- The stream through which we can write data from the Java application to persistence store for storing those data.

Types of streams based on format of the data moving from Java application to File/Console,

1) Binary Stream:- The streams which can read and write data in the form of bytes. It contains data in 0 and 1 format.

2) Character Streams:- The streams which read and write data in the form of characters/text/string.

Binary Stream classes are available from the JDK1.0 version onwards but Character Streams classes are introduced in the JDK1.1 version because some characters like \n, \t, \a e.t.c are not interpreted correctly.

When to choose Binary stream or Character stream?
Binary Stream:- To read/write Number/Object data
Character Stream:- To read/write the text/character/String data

Java IOStream classes

Based on different sources and destinations to read and write data, different sub classes are given. Different sources and destinations are given below,

  • Source:- Keyboard, File, Database, Socket(computer), Object, Array, String, and StringBuffer
  • Destination:- Monitor, File, Database, Socket(computer), Object, Array, String, and StringBuffer

Based on data flow Binary stream are of two types,
1) InputStream:- To read binary data from the persistence store.
2) OutputStream:- To write binary data to the persistence store.

Both InputStream and OutputStream are abstract classes and based on different sources and formats multiple subclasses are there. InputStream class is the superclass for all binary input stream classes. OutputStream class is the superclass for all binary output stream classes.

Similarly, based on data flow Character stream are of two types,

1) Reader:- To read character data from the persistence store.
2) Writer:- To write character data to the persistence store.

Both Reader and Writer are abstract classes and based on different sources and formats multiple subclasses are there. Reader class is the superclass for all Character input stream classes. Writer class is the superclass for all character output stream classes.

InputStream and Reader classes have read() method to read data from a source. In InputStream class it returns one byte at a time and in Reader class, it returns one character at a time.

Similarly. OutputStream and Writer classes have the write() method to write data to a destination. In OutputStream class it writes one byte at a time and in writer class it writes one character at a time.

While storing data in the binary form we can store value 97 as int, long, float, double which requires different byte size but in character form, it can be stored only in Unicode character form.

Classes

Classes related to write/read binary data

Classes to write binary data,
OutputStream (Superclass)

Classes to read binary data,
InputStream (Superclass)

  • FileInputStream
  • ByteArrayInputStream
  • FilterInputStream
  • ObjectInputStream
  • PipedInputStream
  • SequenceInputStream
  • StringBufferInputStream

Classes related to write/read character or text data

Classes to write character/text data,
Writer (Superclass)

Classes to read character/text data,
Reader (Superclass)

Closing the Java IOStream objects

All IOStream classes are a connection based object which uses different resources. That’s why after completion of its uses we should close the connection. Up to 1.4 version every abstract class has its own close() method definition. In the Java1.5 version, a Closeable marker interface was introduced in java.io which contains only the close() method. Therefore every class of java.io package implements the Closaable interface (Upto Java1.6).

In Java 1.7 version AutoCloseable interface was introduced in java.lang to support the try-with-resource concept. The try-with-resource concept is given for closing the connection object automatically after the execution of the try/catch block. The AutoCloseable is a super interface for closing the connection and now the Closeable interface is the subinterface of AutoCloseable.

package java.lang;
public interface AutoCloseable {
    void close() throws Exception;
}
package java.io;
public interface Closeable extends AutoCloseable {
   public void close() throws IOException;
}

All input and output related classes of java.io package implements Closeable interface and Closeable extended from AutoCloseable interface.

Important Points

Since all IOStream classes are connection-based, where one end is a Java application and another end is a persistent store. We must always pass the persistent store information to the constructor. Therefore, none of these classes contains a 0 parameter constructor.

  • To read character data from a file/console use BufferedReader.
  • To write character data from a file/console use PrintWriter.

In order to read/write character/text/string data, the BufferedReader and PrintWriter classes gives high performance compared to other classes.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *