0% found this document useful (0 votes)
3 views6 pages

Java IO Comprehensive Guide

The document provides a comprehensive guide to Java I/O systems, detailing the differences between Text I/O and Binary I/O, including their definitions, efficiency, and use cases. It also covers key classes for Binary I/O, Character I/O, Object I/O (serialization), and Random Access Files, along with examples of how to use them. The guide emphasizes the importance of understanding these I/O operations for effective data handling in Java applications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Java IO Comprehensive Guide

The document provides a comprehensive guide to Java I/O systems, detailing the differences between Text I/O and Binary I/O, including their definitions, efficiency, and use cases. It also covers key classes for Binary I/O, Character I/O, Object I/O (serialization), and Random Access Files, along with examples of how to use them. The guide emphasizes the importance of understanding these I/O operations for effective data handling in Java applications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java I/O Systems Comprehensive

Guide
Text, Binary, Character, Object, and Random Access Files

1. Text I/O vs. Binary I/O

In Java, I/O (Input/Output) operations are categorized based on how data is handled at the
byte level.

Text I/O

Definition: Text I/O involves reading and writing data as a sequence of characters. It
automatically handles character encoding (like ASCII or Unicode/UTF-8) to translate binary
data into human-readable text.

Example: Writing "123" to a file results in the bytes for '1', '2', and '3' being stored.

Binary I/O

Definition: Binary I/O involves reading and writing data exactly as it is stored in memory,
byte by byte. It does not perform any translation or encoding. It is more efficient for non-text
data like images, audio, or encrypted files.

Example: Writing the integer 123 (as a 4-byte int) stores its actual bit representation.

Feature Text I/O Binary I/O

Unit of Data Characters (16-bit Unicode) Bytes (8-bit)

Human
Yes No (usually gibberish in editors)
Readable

Slower (due to encoding/


Efficiency Faster (direct copy)
decoding)
Images, executable files, serialized
Use Case Config files, logs, documents
objects
2. Binary I/O Classes

Binary I/O is handled by the InputStream and OutputStream hierarchies.

Key Classes:

• FileInputStream / FileOutputStream: Used for reading/writing bytes from/to a file.


• DataInputStream / DataOutputStream: Used to read/write primitive Java data types
(int, double, boolean) in a machine-independent way.

Example (DataOutputStream):

try (DataOutputStream out = new DataOutputStream(new


FileOutputStream("[Link]"))) {
[Link](42);
[Link](3.14);
[Link](true);
}

3. Character I/O Classes

Character I/O is handled by the Reader and Writer hierarchies. These classes are specifically
designed to handle 16-bit Unicode characters.

Key Classes:

• FileReader / FileWriter: Direct character streams for files.


• BufferedReader / BufferedWriter: Wraps other readers/writers to provide efficient
buffering and methods like readLine().

Example (BufferedReader):

try (BufferedReader reader = new BufferedReader(new


FileReader("[Link]"))) {
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
}
4. Object I/O (Serialization)

Definition: Object I/O allows you to read and write entire Java objects to a stream. This
process is called Serialization (writing) and Deserialization (reading).

Requirement: A class must implement the [Link] interface (a


marker interface) to be written using Object I/O.

Key Classes:

• ObjectOutputStream: Converts an object into a byte stream.


• ObjectInputStream: Reconstructs an object from a byte stream.

Example:

// Writing an Object
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("[Link]"));
[Link](new MyClass("Test"));

// Reading an Object
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("[Link]"));
MyClass obj = (MyClass) [Link]();

5. Random Access Files

Definition: Unlike the previous streams which are sequential (you must read from the start
to the end), a RandomAccessFile allows you to jump to any location in the file to read or
write.

It behaves like a large array of bytes stored in the file system. There is a "file pointer" that
tracks the current position.

Key Methods:

• seek(long pos): Moves the file pointer to a specific byte offset.


• getFilePointer(): Returns the current position.
• length(): Returns the size of the file.

Example:

try (RandomAccessFile raf = new RandomAccessFile("[Link]", "rw")) {


[Link](100); // Writes at start
[Link](10); // Jump to byte 10
[Link]("Hello"); // Write string at byte 10
}

You might also like