Simple File Handling Program In Java

2020. 3. 2. 07:55카테고리 없음

So far the operations using Java programs are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file.What is File Handling in Java?A file is a container which is used to store various types of information. Data is permanently stored in secondary memory by creating a file with a unique name.

Object serialization is a mechanism to manipulate Java objects (POJO) to and from a stream of bytes. Serialization, in particular, refers to its representation as a sequence of bytes.

Simple File Read Program In Java

These bytes contains an object's data, type information of the object, and also the type of data content in the object. An object, once serialized, can be persisted in a file or used in an application that communicate via network. The reverse process, called deserialization, is used to re-create the same object in memory from the stream of bytes. Data communication via objects rather than a sequence of bytes is convenient and efficient in both file handling and network communication. In this article, we shall explore the serialization/deserialization process with the help of a file handling mechanism. Conceptually, serialization in network programming also follows the same idea.

The Need for Objects to be SerializedBecause serialization converts an object to a stream of bytes and deserialization re-creates the object in memory, the basic idea of operation remains the same as it in object persistence or a network transfer. Note that serialization is not the only answer to transform objects into a stream of bytes but a standard and convenient way to do so.

Simple File Handling Program In Java

Otherwise, one had to re-create objects from the sequence of bytes from scratch in every application. For example, in a simple file handling program without serialization, when an instance variable for a certain object were to output to a disk file, the type information of object data gets lost. That means, a value, say '55,' is read back from a file, there is no way to tell if the value came from a Integer, a String, or a Double. Also, it is not possible to determine whether the separator used in the file between values is a space character or something else. Are there single records in a line or multiple records per line in the file? This information is necessary for re-creating the object, especially by the program that reads the data.

If, however, the program knows the object type exactly, perhaps it's all right and possible to read into the correct object. But in practice, it's often not so. It is not possible to know precisely how data is stored in a file unless type information of the object with its content are also maintained systematically in the file.

Simple File Reader Program In Java

Implementing Object SerializationSerialization of a object starts with implementing a java.io.Serialization interface. Then, to work with the serialized object, we have to initialize the ObjectInputStream and ObjectOutputStream objects with stream objects. ObjectInputStream implements the ObjectInput interface and ObjectOutputStream implements the ObjectOutput interface, respectively. These objects read from and write to file stream objects, such as FileInputStream and FileOutputStream, respectively (in case of file handling).

Both file stream and object streams are basically stream objects wrapped into one another for the purpose of object persistence in a file. The stream object being created wraps another stream object specified as a constructor argument; for example, to wrap FileOuputStream in an ObjectOutputStream, we pass a FileOutputStream object to the ObjectOutputStream's constructor. The method writeObject, responsible for streaming object bytes, takes an object that implements a Serializable interface as an argument. In a way, an object that implements a Serializable interface is tagged to transform into a sequence of bytes retaining not only the object values but also the type information of the object and its content as well. Serialization acts as a tagging interface and does not have any method of its own. ObjectInputStream/ObjectOuputStream will not input/output an object unless it is marked serializable. A serializable class must ensure that the instance variables of the class must also be serializable; if this is not so, they must be declared as transient to indicate non serializability and they should be ignored during the serializability process.

This situation occurs in cases such as in serializing reference types. All primitive types of variables in Java are serializable including array objects. However, for some reference types or an array of reference types, it may occur that the referred object is not serializable. Object Serialization in a Sequential Access FileLet us create and manipulate a simple sequential access file using object serialization to delineate the idea further.