What is File Handling in Java?
File Handling in Java allows you to create, read, write, update, and delete files using the java.io and java.nio packages.
Commonly used classes:
FileFileWriter/BufferedWriterFileReader/BufferedReaderScannerPrintWriterFiles(fromjava.nio.file)
Common File Operations
| Operation | Java Class Used |
|---|---|
| Create a file | File |
| Write to a file | FileWriter, BufferedWriter, PrintWriter |
| Read from a file | FileReader, BufferedReader, Scanner |
| Delete a file | File |
Example 1: Create a File
import java.io.File;
import java.io.IOException;
public class CreateFileExample {
public static void main(String[] args) {
try {
File file = new File("example.txt");
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Example 2: Write to a File
import java.io.FileWriter;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("example.txt");
writer.write("Hello, this is Java file handling!\nLet's learn it.");
writer.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Example 3: Read from a File
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFileExample {
public static void main(String[] args) {
try {
File file = new File("example.txt");
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String line = reader.nextLine();
System.out.println(line);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
e.printStackTrace();
}
}
}
Example 4: Delete a File
import java.io.File;
public class DeleteFileExample {
public static void main(String[] args) {
File file = new File("example.txt");
if (file.delete()) {
System.out.println("Deleted the file: " + file.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}
Additional: Using BufferedReader for Efficient Reading
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("example.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Best Practices
- Always close file streams after use (
.close()). - Use
try-with-resources(Java 7+) for automatic closing. - Handle exceptions like
IOExceptionorFileNotFoundException. - Use buffered readers/writers for large files.
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
