Unit-IV I/O PROGRAMMING
1. Text and Binary I/O
Text I/O is used to read and write data in human-readable form such as characters and strings. It
uses character streams like FileReader and FileWriter. Binary I/O deals with raw byte data
and is used for files like images, audio, etc. It uses FileInputStream and FileOutputStream.
Text I/O is easy to understand and debug, whereas binary I/O is faster and more efficient. Choice
depends on application requirements.
Example (Text I/O):
import [Link].*;
class TextIO {
public static void main(String[] args) throws Exception {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello World");
[Link]();
}
}
2. Binary I/O Classes
Binary I/O uses byte streams to handle data at byte level. Important classes include
FileInputStream, FileOutputStream, BufferedInputStream, and BufferedOutputStream.
These classes are efficient for large data handling. DataInputStream and DataOutputStream
allow reading/writing primitive data types. They are widely used in file processing and
networking. Binary I/O is not human-readable.
Example:
import [Link].*;
class BinaryIO {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("[Link]");
[Link](100);
[Link]();
}
}
3. Object I/O (Serialization)
Object I/O is used to store and retrieve objects using serialization. A class must implement
Serializable interface. ObjectOutputStream is used to write objects and
ObjectInputStream to read them. It preserves object state. It is useful for saving application
data permanently. Deserialization restores the object.
Example:
import [Link].*;
class Student implements Serializable {
int id;
String name;
Student(int i, String n) { id = i; name = n; }
}
class ObjectIO {
public static void main(String[] args) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("[Link]"));
[Link](new Student(1, "Rahul"));
[Link]();
}
}
4. Random Access Files
RandomAccessFile allows reading and writing at any position in a file. It supports both read and
write modes. The seek() method moves the file pointer to a specific position. It is useful in
database systems and large file handling. It provides flexibility compared to sequential access. It
works with binary data.
Example:
import [Link].*;
class RAF {
public static void main(String[] args) throws Exception {
RandomAccessFile raf = new RandomAccessFile("[Link]", "rw");
[Link]("Hello");
[Link](0);
[Link]([Link]());
[Link]();
}
}
JAVA COLLECTIONS
5. Introduction to Java Collections
Java Collections Framework (JCF) is a set of classes and interfaces used to store and manipulate
data. It includes structures like lists, sets, and maps. It reduces coding effort by providing ready-
made methods. It improves performance and scalability. It is part of [Link] package.
Collections support dynamic memory allocation.
6. Overview of Java Collection Framework
The framework consists of interfaces like List, Set, Map, and classes like ArrayList, HashSet,
HashMap. It also provides algorithms like sorting and searching. Iterators are used to traverse
elements. It supports generics for type safety. It improves reusability and efficiency. It is widely
used in modern Java applications.
7. ArrayList
ArrayList is a dynamic array that implements the List interface. It allows duplicate elements
and maintains insertion order. It provides fast access using index. It is not synchronized. It is
flexible and widely used. It automatically resizes when elements are added.
Example:
import [Link].*;
class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
[Link]("A");
[Link]("B");
[Link](list);
}
}
8. Vector
Vector is similar to ArrayList but synchronized. It is thread-safe. It is part of legacy classes in
Java. Due to synchronization, it is slower than ArrayList. It allows duplicate elements. It
maintains insertion order.
9. Hashtable
Hashtable stores key-value pairs. It does not allow null keys or values. It is synchronized and
thread-safe. It uses hashing technique for fast retrieval. It is part of legacy classes. It is slower
than HashMap.
Example:
import [Link].*;
class HashTableDemo {
public static void main(String[] args) {
Hashtable<Integer, String> ht = new Hashtable<>();
[Link](1, "One");
[Link](2, "Two");
[Link](ht);
}
}
10. Stack
Stack follows LIFO (Last In First Out) principle. It extends Vector class. It provides methods
like push(), pop(), and peek(). It is used in recursion, parsing, and undo operations. It is
simple and widely used.
Example:
import [Link].*;
class StackDemo {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
[Link](10);
[Link](20);
[Link]([Link]());
}
}