Week 11
Week 11
In Java, file handling is performed using classes from the `java.io` package. These classes allow reading
and writing data from files.
- **`FileReader`** and **`FileWriter`** are used for reading and writing character data in Java.
##### **FileReader**
```java
import java.io.FileReader;
import java.io.IOException;
int ch;
System.out.print((char) ch);
}
} catch (IOException e) {
```
##### **FileWriter**
```java
import java.io.FileWriter;
import java.io.IOException;
} catch (IOException e) {
}
}
```
---
An **exception** is an event that disrupts the normal flow of a program. It occurs during runtime due
to various reasons like invalid input, file not found, or division by zero.
Java provides a robust exception-handling mechanism that allows developers to handle errors
gracefully.
- Examples:
- `IOException`
- `SQLException`
- `FileNotFoundException`
- Examples:
- `NullPointerException`
- `ArithmeticException`
- `ArrayIndexOutOfBoundsException`
---
Java provides a structured way to handle exceptions using `try`, `catch`, `finally`, `throw`, and `throws`
keywords.
- The `try` block contains the code that may throw an exception.
```java
System.out.println(result);
} catch (ArithmeticException e) {
```
```java
import java.io.FileReader;
import java.io.IOException;
FileReader fr = null;
try {
fr = new FileReader("input.txt");
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
} catch (IOException e) {
} finally {
try {
System.out.println("\nFile closed.");
} catch (IOException e) {
```
```java
checkAge(16);
```
```java
import java.io.FileReader;
import java.io.IOException;
fr.close();
}
public static void main(String[] args) {
try {
readFile();
} catch (IOException e) {
```
---
### **Summary**
These concepts are essential for robust Java programming, ensuring both smooth execution and proper
error handling.
Java provides built-in support for file handling and exception management to ensure smooth execution
of programs. Below are the key concepts related to **Files and I/O Streams** and **Exception
Handling** in Java.
---
Java provides the `java.io` package for handling files and input/output (I/O) operations. The two primary
types of streams are:
- **Byte Streams**: Used for handling binary data (e.g., images, audio files). Classes include
`FileInputStream` and `FileOutputStream`.
- **Character Streams**: Used for handling text data. Classes include `FileReader` and `FileWriter`.
import java.io.FileReader;
import java.io.IOException;
int ch;
System.out.print((char) ch);
} catch (IOException e) {
```
```java
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
} catch (IOException e) {
```
**Key Features:**
- They handle character data, unlike `FileInputStream` and `FileOutputStream`, which handle byte data.
---
An **exception** is an event that disrupts the normal execution of a program. In Java, exceptions are
objects that describe an error that occurs during program execution.
---
- Examples:
```java
import java.io.FileReader;
import java.io.IOException;
try {
FileReader fr = new FileReader("nonexistentfile.txt");
} catch (IOException e) {
```
- Examples:
- `NullPointerException`
- `ArrayIndexOutOfBoundsException`
```java
int a = 10, b = 0;
System.out.println(result);
}
```
---
Java provides mechanisms to **handle exceptions gracefully** without crashing the program. Exception
handling is implemented using:
- The `try` block contains the code that may generate an exception.
- The `catch` block handles the exception and prevents program termination.
```java
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
```
---
```java
import java.io.FileReader;
import java.io.IOException;
FileReader fr = null;
try {
fr = new FileReader("input.txt");
} catch (IOException e) {
} finally {
try {
if (fr != null) {
fr.close();
System.out.println("File closed.");
}
} catch (IOException e) {
```
---
```java
```
---
- The `throws` keyword is used to declare exceptions that a method may throw.
```java
import java.io.FileReader;
import java.io.IOException;
fr.close();
try {
readFile();
} catch (IOException e) {
```
---
## **5. Summary**
| **Concept** | **Description** |
|------------|----------------|
By handling exceptions properly, Java ensures that applications **run smoothly**, even when
unexpected errors occur.