IOStream in Java

IOStream in Java | In Java, All pre-defined classes belonging to the java.io package are 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 File Handling or File IO or File IO stream. The java.io package does not only have pre-defined classes for reading and storing data to a file but also has 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 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 is 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 is 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 operations.

  1. Persistence Store:- The environment that allows us to store data permanently. We can store data permanently in three places:- File, Database, Remote Computer.
  2. Persistence:- The process of storing and managing data permanently in a persistence store for a long time.
  3. Persistence Logic:- The logic that persists data in a persistence store. Example:- IOStreams code, JDBC code, Hibernate code, Spring JDBC code, Networking code.
  4. Persistence Operations:- Insert, update, delete, select operations are called persistence operations. These are also called CRUD operations. C=> create, R=> read, U=> update, D=> delete.

Persistence Technologies in Java

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 & scores, score/power given to players, and e.t.c. 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 a reading problem, inside a file we don’t have a searching algorithm. Flat files are not used to store business value rather they 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 the output.

Types of streams based on data flow,

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

Types of streams based on the 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 in the java.io package. Different sources and destinations can be:-

  • 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 streams 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 streams 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 the 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 sizes but in character form, it can be stored only in Unicode character form.

IOStream Classes in Java

Classes to write binary data,

Classes to read binary data,

  • InputStream (Superclass)
    • FilterInputStream
    • ObjectInputStream
    • PipedInputStream
    • SequenceInputStream
    • StringBufferInputStream

Classes to write character/text data,

Classes to read character/text data,

Closing the Java IOStream objects

All IOStream classes are 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 Closeable 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;
}

Now, all input and output related classes of java.io package implement 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.

Conclusion:- In order to read/write character/text/string data, the BufferedReader and PrintWriter classes give high performance compared to the 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 do you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “IOStream in Java”

  1. Hariharasudhan V

    It was easy to understand as well as it is useful for my studies. I was searched and study about this topic on some other websites but only this website gives an understanding of this topic.

Leave a Comment

Your email address will not be published.