Java Input Output Streams
What the actual functionalities of OOS and OIS?
Performing serialization and deserialization
What is Serialization?
Serialization is the process of Converting an object into stream of bytes
and sending them to underling outputstream.
Using serialization we can store object state permanently in a destination
EX: File or Remote Computer(RMI, Socket Programing).
Serialization operation is performed by writeObeject().
What is Deserialization?
Deserialization is the process of Converting stream of bytes into original object.
Deserialization operation is performed by readObeject().
Rule on Serialization?
Serializable is a marker interface.
only Serializable type objects are serializable.
It provides special permition or identity to JVM to serializable object.
If the given object isn't type serializable interfacethen writeObeject() will
throw an unchecked exception called NotSeriableException.
What are the methods with serializable interface?
No methods are there in serializable interface
as a marker interface.
Ex: Serializable, Clonable Remote, SingleThreadModel..
Important Notes:
Classes must implement Serializable interface.
Fields marked transient will not be serialized.
Static fields are not serialized.
Java provides serialVersionUID to maintain version control during deserialization.
When NOT to use Java Serialization:
If you need language-independent formats (prefer JSON, XML, or Protobuf).
For security-sensitive data transfer (Java serialization can be vulnerable if misused).
When you need fine control over the serialized format.
What is Streams and types?
Stream is a logical connection between java program and a file.
2 types of streams
Input stream: it recieve or read data
Output stream: it send or write data to some other place. like
System.in/System.out/System.error.
How a java application can store or read data from a file?
Using Stream Object.
What are limitation of FIS and FOS?
FIS and FOS allows us to read and write data only in the format of bytes.
It is not possible to read or write data in the format of primitive or objects data.
Solution: we should use filter input stream and output stream classes.
Explain Externalizable?
Externalizable is an interface that extends Serializable interface
and send data into streams in compressed format. It has 2 methods
writeExteranal(ObjectOutput out)
readExteranal(ObjectInput out)
What is the difference between byte streams and character streams in Java?
Byte streams (InputStream, OutputStream) handle raw binary data.
Character streams (Reader, Writer) handle character data using Unicode.
What is the difference between FileInputStream and BufferedInputStream?
FileInputStream reads byte by byte directly from file.
BufferedInputStream uses a buffer for improved performance (less disk I/O).
How does FileReader differ from FileInputStream?
FileReader reads character data (text files).
FileInputStream reads binary data (images, audio, etc.).
Why are streams in Java abstract classes instead of interfaces?
Because streams provide partial implementation which can be extended by subclasses,
especially for methods like read(), write(), etc.
What is the use of flush() method in OutputStreams or Writers?
It forces any buffered output bytes or characters to be written immediately
to the underlying output stream.
What happens if you don't close a stream in Java?
It can lead to resource leaks and file locks. Modern
Java uses try-with-resources to avoid this.
What is the try-with-resources statement?
Introduced in Java 7, it auto-closes resources
(like streams, readers, writers) that implement AutoCloseable.
Difference between InputStreamReader and BufferedReader?
InputStreamReader: Converts byte stream to character stream.
BufferedReader: Buffers character input,
offers methods like readLine() for efficient reading.
What is the purpose of DataInputStream and DataOutputStream?
These allow you to read/write primitive data types (int, long, float, etc.)
in a machine-independent way.
How does ObjectInputStream and ObjectOutputStream work?
Used for serialization — converting objects to byte streams and back.
Requires objects to implement Serializable.
How would you read a large file efficiently in Java?
Use BufferedReader with a large buffer size.
Or FileChannel with memory-mapped files (for very large files).
How do you copy the contents of one file to another in Java?
With FileInputStream and FileOutputStream in a loop.
Or using Files.copy() from java.nio.file.
What happens if you serialize a non-serializable object?
NotSerializableException is thrown at runtime.
Can you customize serialization?
Yes, using writeObject() and readObject() methods.
What’s the difference between write() and writeBytes() in DataOutputStream?
write() writes a byte.
writeBytes(String) writes low-order eight bits of each
character (platform-dependent encoding).
Is Java I/O stream thread-safe?
No. Most I/O classes are not thread-safe,
synchronization must be handled externally.
How do buffered streams improve performance?
By reducing the number of native I/O calls;
they read/write data in chunks.
What is the role of PipedInputStream and PipedOutputStream?
They are used for thread communication via streams
(producer-consumer model).