File Handling in Java - Detailed Explanation
1. What is File Handling?
File handling allows you to create, read, update, and delete files from your Java program. Java provides two major APIs:
[Link] (traditional) and [Link] (modern, powerful, introduced in Java 7).
2. The File Class ([Link])
This class is used to represent file and directory pathnames. It is used for metadata and directory operations.
Example:
import [Link];
import [Link];
public class FileClassExample {
public static void main(String[] args) {
File file = new File("[Link]");
try {
if ([Link]()) {
[Link]("File created: " + [Link]());
} else {
[Link]("File already exists.");
}
[Link]("Absolute path: " + [Link]());
} catch (IOException e) {
[Link]();
}
}
}
3. Reading from a File
a) Using FileReader and BufferedReader:
import [Link].*;
public class ReadTextFile {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
b) Using FileInputStream:
import [Link].*;
public class ReadBinaryFile {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("[Link]");
int data;
while ((data = [Link]()) != -1) {
[Link]((char) data);
}
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
4. Writing to a File
a) Using FileWriter and BufferedWriter:
import [Link].*;
public class WriteToFile {
public static void main(String[] args) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("[Link]"));
[Link]("Hello Java!");
[Link]();
[Link]("File handling is powerful.");
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
b) Using FileOutputStream:
import [Link].*;
public class WriteBinaryFile {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("[Link]");
String content = "Hello in bytes!";
[Link]([Link]());
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
5. Appending to a File
import [Link].*;
public class AppendToFile {
public static void main(String[] args) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("[Link]", true));
[Link]("Appended line.");
[Link]();
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
6. File Deletion
import [Link];
public class DeleteFile {
public static void main(String[] args) {
File file = new File("[Link]");
if ([Link]()) {
[Link]("Deleted successfully.");
} else {
[Link]("Failed to delete file.");
}
}
}
7. Modern File Handling using [Link]
import [Link].*;
import [Link];
public class NIOFileExample {
public static void main(String[] args) {
Path path = [Link]("nio_example.txt");
try {
[Link](path, "Hello NIO!".getBytes());
String content = new String([Link](path));
[Link](content);
} catch (IOException e) {
[Link]();
}
}
}
8. Reading All Lines at Once
import [Link].*;
import [Link];
import [Link];
public class ReadAllLines {
public static void main(String[] args) throws IOException {
List<String> lines = [Link]([Link]("[Link]"));
for (String line : lines) {
[Link](line);
}
}
}
9. Try-with-Resources
import [Link].*;
public class SafeRead {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("[Link]"))) {
String line;
while ((line = [Link]()) != null)
[Link](line);
} catch (IOException e) {
[Link]();
}
}
}
10. Common Exceptions
- FileNotFoundException: File not found when trying to read.
- IOException: General input/output error.
- SecurityException: Access to file denied.
- NullPointerException: File object is null.