
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between FileInputStream and ObjectInputStream in Java
When working with file dealing in Java, there are different classes accessible to examine information from records. Two commonly utilized classes are FileInputStream and ObjectInputStream. Whereas both classes serve the reason of reading information from records, they contrast in their approaches and functionalities. In this article, we are going to investigate the contrasts between FileInputStream and ObjectInputStream and get it when to utilize each of them.
Syntax
Before delving into the differences, let's understand the syntax of FileInputStream and ObjectInputStream ?
FileInputStream syntax
FileInputStream fis = new FileInputStream("file.txt");
ObjectInputStream syntax
FileInputStream fis = new FileInputStream("file.txt"); ObjectInputStream ois = new ObjectInputStream(fis);
Explanation of Syntax
The FileInputStream class is capable for reading raw bytes from a record. It takes a record title or record descriptor as a parameter and opens a stream for reading. On the other hand, ObjectInputStream could be a higher-level course that amplifies FileInputStream. It provides extra functionality to studied serialized objects from a record. To utilize ObjectInputStream, we to begin and create an instance of FileInputStream and after that pass it as a parameter to the ObjectInputStream constructor.
Approach 1: FileInputStream
The FileInputStream class works at the byte level and is essentially utilized for reading crude information from a record. It gives a stream of bytes that can be read consecutively. Let's understand the approach in detail ?
Algorithm
Create an instance of FileInputStream by providing the file name or file descriptor.
Create a buffer or an array of bytes to store the data read from the file.
Use the read() method of FileInputStream to read data from the file into the buffer.
Process the data from the buffer as required.
Example
import java.io.FileInputStream; import java.io.IOException; public class FileInputStreamExample { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("file.txt"); byte[] buffer = new byte[1024]; int bytesRead = fis.read(buffer); while (bytesRead != -1) { // Process the data from the buffer System.out.println(new String(buffer, 0, bytesRead)); bytesRead = fis.read(buffer); } fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
This is an example text file. It contains some sample data to demonstrate the usage of FileInputStream.
In this code illustration, we make an instance of FileInputStream utilizing the record title "file.txt". We too make a byte array named "buffer" to store the information studied from the record. Interior the whereas loop, we read information from the record into the buffer utilizing the read() strategy of FileInputStream. We at that point prepare the information from the buffer, in this case, basically printing it to the comfort. The loop proceeds until the read() strategy returns -1, demonstrating the conclusion of the record. Finally, we close the FileInputStream to release the system resources.
Approach 2: ObjectInputStream
The ObjectInputStream class is utilized to read serialized objects from a file. It gives higher-level functionality to handle object serialization and deserialization. Let's explore the approach ?
Algorithm
Create an instance of FileInputStream by providing the file name or file descriptor.
Create an instance of ObjectInputStream by passing the FileInputStream instance as a parameter.
Use the readObject() method of ObjectInputStream to read serialized objects from the file.
Process the objects as required.
Example
import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class ObjectInputStreamExample { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("file.ser"); ObjectInputStream ois = new ObjectInputStream(fis); Object obj = ois.readObject(); // Process the object as required System.out.println(obj.toString()); ois.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }
Output
This is an example text file. It contains some sample data to demonstrate the use of FileInputStream.
In this code illustration, we make an instance of FileInputStream utilizing the record title "file.txt". We then make an instance of ObjectInputStream by passing the FileInputStream instance as a parameter. Utilizing the readObject() strategy of ObjectInputStream, we studied the serialized object from the file. We can then handle the object as required. In this case, we essentially print the protest to the support utilizing the toString() strategy. Finally, we close the ObjectInputStream to release the system resources.
Difference Between FileInputStream and ObjectInputStream in Java
Points of Difference |
FileInputStream |
ObjectInputStream |
---|---|---|
Purpose |
Used for reading raw bytes from a file. |
Used for reading serialized objects from a file. |
Stream Type |
Operates at the byte level, providing a stream of bytes. |
Higher-level class that extends FileInputStream. |
Data Processing |
Requires additional processing to convert bytes into desired data types. |
Handles object serialization and deserialization automatically. |
Usage with Non-serialized Data |
Suitable for reading any type of data from a file. |
Not intended for reading non-serialized data. |
Dependencies |
Directly depends on FileInputStream class. |
Extends FileInputStream class for added functionality. |
Conclusion
In outline, the FileInputStream and ObjectInputStream classes in Java give diverse approaches for reading information from records. FileInputStream is utilized for reading raw bytes, whereas ObjectInputStream is utilized for reading serialized objects. Understanding the differences between these classes and their particular utilize cases is vital for effective record taking care of in Java. By utilizing the appropriate class based on your necessities, you'll successfully read and handle information from records in your Java applications.