CS18000: Problem Solving and Object-Oriented Programming - File I/O and Exception Handling
CS18000: Problem Solving and Object-Oriented Programming
Basics of File I/O
- External Communication: File Input/Output (I/O) allows programs to communicate with external files and systems.
Persistence of File Storage
- RAM Characteristics: Memory is volatile; data is lost during crashes or reboots.
- Files: Provide persistent storage, allowing programs to save and recover data.
- File I/O Benefits:
- Recovery from crashes/reboots.
- Input for other programs.
- Diagram Representation:
- C -> Program Input: Data can be sourced from a file.
- Program Output ->: Data may be directed to a file.
Files and Java
- Platform Independence: Java aims for compatibility across operating systems but is subject to some limitations based on OS-dependent aspects.
- File Class: Used to manage files and directories.
- Abstraction Layers: Java provides three layers for file I/O:
- Low-Level: Raw byte data (e.g.,
FileInputStream, FileOutputStream). - High-Level: Primitive types (e.g.,
DataInputStream, DataOutputStream). - Object I/O: Java objects (e.g.,
ObjectInputStream, ObjectOutputStream).
Buffering
- Purpose: Improves performance by reducing physical disk access by using internal arrays to store data temporarily.
- Strategies:
- Read more data than needed, retain extra for subsequent calls.
- Delay writing until necessary to minimize disk access.
Generic File Operations
- Open:
- Specify mode for reading/writing/appending.
- Read: Transfer data from file to user.
- Write: Transfer data from user to file.
- File Position Adjustment: Set current position in file for reading/writing.
- Close: Flush data, free resources.
File I/O Layers in Java
import java.io.*;
public class LowLevelIO {
public static void main(String[] args) throws IOException {
File f = new File("lowlevel");
FileOutputStream fos = new FileOutputStream(f);
fos.write(42);
fos.close();
FileInputStream fis = new FileInputStream(f);
int i = fis.read();
System.out.printf("Read %d\n", i);
fis.close();
}
}
Exception Handling Basics
- Purpose: Manage error situations and prevent program crashes.
- Try-Catch Structure:
try {
// Statements that may throw
} catch (ExceptionType e) {
// Recovery statements
}
- Throws Clause: A method can declare exceptions without handling it internally.
Exception Class Overview
- Hierarchy:
IOException, FileNotFoundException, ArithmeticException, etc.
- Checked vs. Unchecked Exceptions:
- Checked: Must be handled (e.g.,
FileNotFoundException). - Unchecked: Runtime errors (e.g.,
NullPointerException).
Advanced Exception Handling
public class StudentNotFoundException extends Exception {
public StudentNotFoundException(String message) {
super(message);
}
}
- Catching Multiple Exceptions: Use separate catch blocks for different exception types in order of inheritance.
- Finally Clause: Ensures certain code executes regardless of exceptions.
- Try-With-Resources: Automatically closes resources (e.g., files).
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
// Read operations
}
Conclusion
- Mastery of file I/O and exception handling is crucial for robust Java programming. These features enable data persistence and graceful error recovery, essential in real-world applications.