5.3 Text IO Vs Binary IO Binary IO Classes Motivations and Benefits Defining Generic Classes and Interfaces
5.3 Text IO Vs Binary IO Binary IO Classes Motivations and Benefits Defining Generic Classes and Interfaces
Java provides two main ways to handle file input and output: Text I/O and Binary I/O.
Feature Text I/O Binary I/O
Data Type Reads/writes text (characters) Reads/writes raw bytes
Uses character encoding (e.g.,
Encoding No encoding, stores raw binary data
UTF-8)
Slower (encoding/decoding
Speed Faster (direct byte storage)
overhead)
Usage For text files (e.g., .txt, .csv) For binary files (e.g., .jpg, .mp3, .exe)
Java BufferedReader, BufferedWriter, FileInputStream, FileOutputStream,
Classes FileReader, FileWriter ObjectInputStream, ObjectOutputStream
import java.io.*;
// A serializable class
class Person implements Serializable {
String name;
int age;
T getValue() {
return value;
}
}
4. Generic Interfaces
A generic interface defines a contract that multiple classes can follow.
Example of a Generic Interface
// Generic interface
interface Computable<T> {
T compute(T a, T b);
}