Starting our article’s first example, we have been creating a new java file, “test.java”. After that, we also generated a text file in the same folder.
To read objects of the input stream in a java program, we need to import the output stream. So, we have been importing the “java.io.ObjectInputStream” and “java.io.ObjectOutputStream” packages at the start. Along with that, to insert the data into a file, we need to import the “java.io.FileInputStream” and “java.io.FileInputStream”. We have created a class “Main” holding a single main() function.
The function execution starts from the declaration of an integer variable “d”. To avoid the sudden exit of the program due to errors, we added the try-catch statement of java. The “try” part starts with the initialization of a file output stream object “f” via the FileOutputStream class. We have passed the file name “new.txt” to this object “f”. We have created an Object Output Stream “o” and passed the file object “f” to the ObjectOutputStream class to make a file an output stream.
At the very next line, we have been calling the writeInt() function of the Java output stream via the output stream object “o” to pass it an integer variable “d,” i.e., to save it in a file. On line 12, we have created file input stream “fs” using the FileInputStream class of java by passing it a file “new.txt,” i.e., already have data in it. This file input stream object “fs” has been passed to the newly generated object “os” of ObjectInputStream class to enable reading from the file stream. The println() function from the “System.out” package of the java has been castoff to call the readInt() function via the object input stream object “os” to display the data from the new.txt file. After that, we have been closing the output stream and input stream using their respective objects “o” and “os” using the “close” function of file handling in java. This is a necessary step to close the streams so that no other user can input or output data from a file. Within the catch() statement, we have been using the getStackTrace() function to get an error and display it on the screen via the Exception variable “e”. This program is ready for execution on the shell.
We had just saved our java code file before its execution and used the java keyword instruction to execute the “test.java” file. It returns nothing in return. Also, displaying the data of a text file “new.txt” on the shell using the “cat” instruction displayed a garbage value. This is because, most of the time, the java executor is unable to read the integer value from the file. But most probably, it will display the exact integer value.
On checking the new.txt file by manually opening it, we have seen that a Unicode format of the value has been shown in the file.
If you are unable to display the integer value from the file stream, you can use the writeObject() function in the java program instead of the writeInt() function to write the value “d” to the file stream via the object “o” as displayed. Along with that, you need to cast off the “readObject()” function instead of the readInt() function to display the data from the file input stream.
Updating this code would display the integer value on execution.
Let’s have another value of java programming to cast off the object input stream for the string type value. Thus, we have started this example code with the import of the same FileInputStream, FileOutputStream, ObjectInputStream, and ObjectOutputStream classes of java from the “io” package of the java library. The Main class has its main() function to start executing this code. A string variable “d2” is initialized with a long string value.
The same try-catch statement has been castoff to avoid the error and for smooth execution of this java code. The object “f” for the file output stream has been created via the FileOutputStream class taking “new.txt” as an argument. The file output stream object “f” has been passed to the output stream object “o” that has been created by the use of ObjectOutputStream class. Now, the output stream “o” has been calling the writeObject() function by passing it a string variable “d” to be written in the file “new.txt”.
Then, created a file input stream “fs” using the FileInputStream class by passing it a file name “new.txt,” i.e., to read the data from it. Now, the ObjectInputStream class object “os” would be using the file input stream object “fs” to read the data using the readObject() function of java in the “println” execution statement of the “System.out” class. Then, we tend to close the input and output streams by calling the “close” function, and the catch statement is utilized to get any exceptions to have happened in the try part and avoid the sudden closing of this program execution.
The execution of this java code file and the text file displayed the string value on the console.
The use of ObjectInputStream class is the sole method available for reading substances that use the Serializable or Externalizable protocol. To sum up, we have included all the necessary details to utilize java’s objectinputstream class to read data from the file object input stream. For this, we have been casting two different java code examples here. The first example utilizes the integer type input variable value, while the second illustration has been casting off the string variable value, i.e., read from the input file stream.