Home/Notes/I/O & Streams
Board Exam Notes

I/O & Streams Notes

Questions

1–2 questions in university theory papers

Difficulty

Easy

Importance

Core foundational topic for Java lab practicals and theory.

Overview

I/O Streams in Java represent the flow of data from a source to a destination, providing a unified way to handle input and output operations regardless of the device. Mastering this topic is essential for understanding how Java applications interact with files and networks, forming the backbone of practical data handling in academic projects and university exams.

Byte Streams vs Character Streams

Byte streams handle raw 8-bit binary data, while character streams are designed to handle 16-bit Unicode characters. Choosing the correct stream type is critical for ensuring data integrity, especially when dealing with different encoding standards.

  • Byte streams use InputStream and OutputStream classes.
  • Character streams use Reader and Writer classes.
  • Character streams automatically handle character set conversion.
  • Byte streams are best for binary data like images or compiled code.
  • Character streams are preferred for human-readable text files.

File I/O Operations

File I/O involves using classes from the java.io package to read from and write to disk files. Exams often test the hierarchy of these classes and the necessity of handling IOExceptions.

  • FileInputStream and FileOutputStream for binary file handling.
  • FileReader and FileWriter for character-based file access.
  • BufferedReader and BufferedWriter for efficient buffered operations.
  • File class provides utility methods to manage file system metadata.
  • Always use try-catch or throws clauses for IOException.

Serialization

Serialization is the process of converting an object's state into a byte stream, allowing it to be stored in a file or sent over a network. This is a favorite exam topic, focusing on the Serializable interface and object persistence.

  • Class must implement the java.io.Serializable marker interface.
  • ObjectOutputStream is used for serialization.
  • ObjectInputStream is used for deserialization.
  • The transient keyword prevents a field from being serialized.
  • static variables are not serialized as they belong to the class.

Exam Tip

Always remember that SerialVersionUID is essential for version control during deserialization; mentioning it in your answer will boost your marks significantly.

Common Mistakes

  • Confusing the class hierarchy of byte-oriented streams with character-oriented streams.
  • Forgetting to implement the Serializable interface when attempting to serialize custom objects.
  • Neglecting to close streams in a finally block, leading to memory leaks or file locks.

More Revision Notes

Ready to test yourself?

Play topic-wise I/O & Streams questions in Aspirant Arcade — gamified MCQ practice.

Download Free