- IOException: When performing, this exception may be thrown if there is an input or output error.
- ClassNotFoundException: This error may occur if the class for which the objects were serialized is not found.
- NotActiveException: This error message may appear if the stream is not open and ready to read objects. Next, we will go through the syntax and example of the ObjectInputStream Class’s Readfields() method.
Syntax of Readfields() Method in Java in Ubuntu 20.04
The syntax of the java supports for the Readfields() function is as follows:
The GetFields object checks the fields that may be accessed for this class using the ObjectStreamClass of the current object. During this specific call to the class’s readObject method, the GetFields object provided by readFields is the only one that is valid. Only once readFields has been invoked can more data be read straight from the stream.
Parameter of readFields() method:
There are no parameters that it accepts.
Return type of readFeilds() method:
ObjectInputStream is the method’s return type. When you call GetField, it will return an object called GetField that indicates that the object’s permanent fields are not serialized.
Example 1: Using the ReadField() Method in Java in Ubuntu 20.04
The readFields() function of the java.io.ObjectInputStream class is demonstrated in the example below.
Here, we have included the java package “java.io” with the keyword “import”. Then, we established the class “RF1” with the “public” modifier. We have constructed the main method inside the class “RF1” that also throws the “exception” because the java “Exception” class is also defined with the main method if it occurred. Inside the main body, we have the declaration of the objects as “FOS” by the FileOutputStream, the “Obj” by the ObjectOutputStream, “FIS” object by the FileInputStream, and the “OIS” object by the class “ObjectInputStream”.
The objects “FIS” and “FOS” takes the file “File.txt” as input. The object “Obj” and “OIS” is used to show the stream of these files. Then, we utilize the writeObject() method with the instance “Obj,” which is called the ReadFields() function inside it. The writeObject writes the integers of the instance “Obj”. The stream will be flushed by the flush() method, as we have added it after the writeObject method. After this, we have an instance “RF” of the ReadFeilds that called the readObject method to read the Object “OIS” and print the fields.
Next, we have a static class, “ReadFields,” that implements a serializable class, then declares the string “MyStr” and sets it with the string value. There, we have called the method “readObject”, which defines the instance “s” for the object input stream. The readFields() method then read the fields and return them with the access of the return method.
The java compiler, without any IOException, displays the fields from the readFields() method.
Example 2: Using the ReadFeilds Method With the Validation of Strings in Java in Ubuntu 20.04
As we have seen in an earlier example, that readFields() method returned the field after reading it. Now, we are performing the same operation but with a complex scenario. We are going to validate the first string and then display the second one.
We have constructed the class “RF2,” which has the main method. Within the brackets of the main method, we have defined the string as “s1” and have the value with string representation. Then, we used the try-catch block. The try block has the creation of file “File.txt” by calling the class “FileOuputStream”. The instance is defined as “filein” for the input of the stream that is passed in the “fileout” object.
Then, we called the methods “writeObject” and “writeUTF”. The writeObject method is used to write the stream from the “Demo” operation, and the writeUTF() method takes the “s1” to write the string in the output as a basic stream. After that, we read the object with the readObject method and displayed the string. The readFields method is employed to read the fields of the “s2”. The if statement is called for registerValidation. The catch block will trace the input and output exceptions of the streams.
Next, we have a “Demo” class for serialization of the string and for the validation of deserialized objects, and we have also implemented the ObjectInputValidation class. Here, we have set the string value for the “s2” variable. We have a method “readObject” for this class which has the readFields method to read fields and the registerValidation method. Then, return the string inside the “s2” to the main method.
Compiling and running the aforementioned script of java will result in the following outcome.
Example 3: Using the getFields() Method to get the Field of the Class in Java in Ubuntu 20.04
The public field of this class, together with its members, can be obtained using the getField() function of the “java.lang.Class” class. The function returns Field objects that represent the requested field of this class.
Our class “GetField” is created with the object “MyObj” inside it. Then, we employed the main method inside the class. There, we have a ClassName variable that invoked the forName() method. The object linked to the class will be returned by this method. After that, we utilized the getField() method to get the class field.
After compiling the code, we got the class object name and the field of the class below.
To read serialized data for network transmission, utilize the readFields() methods. The serializable field’s values are read from the stream using the readFields method, which then makes them accessible through the GetField class. A serializable class readObject method is the only place from which the readFields method can be called. Moreover, we have introduced another method that is getField() which returns the field name of the specified class.