File Handling in Java
Understanding file operations with
example programs
Introduction to File Handling
• • File handling allows programs to read/write
data permanently.
• • It is part of [Link] and [Link] packages.
• • Common classes used:
• - File
• - FileReader / FileWriter
• - BufferedReader / BufferedWriter
• - FileInputStream / FileOutputStream
• - Scanner
The File Class
• • The File class represents files or directories.
• • It provides methods to create, delete, and
get file information.
• Example:
• File obj = new File("[Link]");
• if([Link]()) {
• [Link]("File exists");
• } else {
Writing to a File Example
• import [Link];
• import [Link];
• public class WriteToFile {
• public static void main(String[] args) {
• try {
• FileWriter writer = new
FileWriter("[Link]");
• [Link]("Hello, File Handling in
Reading from a File Example
• import [Link];
• import [Link];
• import [Link];
• public class ReadFromFile {
• public static void main(String[] args) {
• try {
• File file = new File("[Link]");
• Scanner reader = new Scanner(file);
Summary
• • File handling enables persistent data
storage.
• • Key classes: File, FileWriter, FileReader,
Scanner.
• • Always handle exceptions like IOException.
• • Close files after use to avoid resource leaks.