Ugenz Academy/Tutorial

Filehandling

4 min read633 words

Reading progress

Keep learning

File Handling in Java

File handling in Java is used to create, read, write, update, and delete files. Java provides several classes in the java.io and java.nio packages for performing file operations.

File handling is useful for storing data permanently instead of keeping it only in memory.

Common File Operations

  1. Create a file
  2. Write data to a file
  3. Read data from a file
  4. Append data
  5. Delete a file

1. Creating a File

The File class can be used to create and manage files.

import java.io.File; import java.io.IOException; class Main { public static void main(String[] args) { try { File file = new File("data.txt"); if (file.createNewFile()) { System.out.println("File created successfully"); } else { System.out.println("File already exists"); } } catch (IOException e) { System.out.println("An error occurred"); } } }

2. Writing to a File

The FileWriter class is commonly used to write text into a file.

import java.io.FileWriter; import java.io.IOException; class Main { public static void main(String[] args) { try { FileWriter writer = new FileWriter("data.txt"); writer.write("Hello, Java!"); writer.close(); System.out.println("Data written successfully"); } catch (IOException e) { System.out.println("An error occurred"); } } }

Output in data.txt

Hello, Java!

3. Reading a File

The FileReader or BufferedReader class can be used to read text from a file.

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; class Main { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader("data.txt")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { System.out.println("An error occurred"); } } }

Output

Hello, Java!

4. Appending Data

Appending means adding new data to the existing contents of a file.

import java.io.FileWriter; import java.io.IOException; class Main { public static void main(String[] args) { try { FileWriter writer = new FileWriter("data.txt", true); writer.write("\nWelcome to Java."); writer.close(); System.out.println("Data appended successfully"); } catch (IOException e) { System.out.println("An error occurred"); } } }

The true parameter tells FileWriter to append data instead of replacing the existing content.

5. Deleting a File

The delete() method can be used to delete a file.

import java.io.File; class Main { public static void main(String[] args) { File file = new File("data.txt"); if (file.delete()) { System.out.println("File deleted successfully"); } else { System.out.println("File not found"); } } }

Common File Handling Classes

ClassPurpose
FileCreates and manages file information
FileReaderReads character data
FileWriterWrites character data
BufferedReaderEfficiently reads text
BufferedWriterEfficiently writes text
FileInputStreamReads byte data
FileOutputStreamWrites byte data

Character Stream vs Byte Stream

Character Stream

Used mainly for text data.

Examples:

FileReader FileWriter

Byte Stream

Used for binary data such as images, audio, and video.

Examples:

FileInputStream FileOutputStream

Advantages of File Handling

  • Stores data permanently.
  • Allows data to be reused later.
  • Helps manage large amounts of data.
  • Supports reading and writing files.
  • Useful for storing application data.
  • Provides support for text and binary files.

Key Points

  • File handling is mainly provided through java.io and java.nio.
  • File is used to manage file information and operations.
  • FileWriter writes character data.
  • FileReader reads character data.
  • FileInputStream and FileOutputStream handle byte data.
  • Exceptions such as IOException should be handled during many file operations.

Conclusion

File handling allows Java programs to create, read, write, append, and delete files. It is important for storing information permanently and working with external data. Java provides several classes and packages that make file operations easier and more efficient.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy