File Handling in Java (IO) – Complete Tutorial
Last Updated on: 26th Nov 2025 13:05:40 PM
File Handling is one of the most important concepts in Java, especially for real-world applications where data needs to be stored permanently.
Java provides a powerful IO (Input/Output) package in java.io and java.nio.file for performing operations such as:
-
Creating files
-
Reading files
-
Writing files
-
Appending data
-
Deleting files
-
Handling directories
Java File Handling is widely used in real-life applications like:
-
Saving logs
-
Storing user data
-
Exporting reports
-
Uploading files
-
Reading configuration files
-
Processing large datasets
In Java, File Handling refers to reading and writing data to/from files on the disk.
Java uses Streams to perform file operations.
A Stream is a flow of data — either input (read) or output (write).
Java IO Has 2 Major Types of Streams:
| Stream Type | Description | Example Classes |
|---|---|---|
| Byte Streams | Reads/Writes data in bytes (binary files) | FileInputStream, FileOutputStream |
| Character Streams | Reads/Writes data in characters (text files) | FileReader, FileWriter |
Also, Java File Handling includes classes such as:
-
File -
FileReader -
FileWriter -
BufferedReader -
BufferedWriter -
FileInputStream -
FileOutputStream -
PrintWriter -
Scanner -
Files(NIO) -
Paths(NIO)
Why File Handling Is Important?
Most real-world applications need to store information permanently:
-
A banking system stores transaction logs
-
A school ERP stores student data
-
Android apps use internal file storage
-
Servers store access and error logs
-
Websites store uploaded files
Without File Handling, all data would be lost after program execution.
So Java provides a robust and secure IO system to work with files.
Key Operations in Java File Handling
Java allows:
-
Create a file
-
Write data to file
-
Read data from file
-
Append data
-
Delete a file
-
Create folders (directories)
-
Read all files inside a folder
Below, each topic is explained in detail.
1. The File Class – Creation, Checking, Deleting Files
java.io.File is used to represent files and directories.
Create File Example
import java.io.*;
public class CreateFile {
public static void main(String[] args) {
try {
File f = new File("test.txt");
if (f.createNewFile()) {
System.out.println("File created: " + f.getName());
} else {
System.out.println("File already exists");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
File created: test.txt
2. Writing to a File using FileWriter
import java.io.*;
public class WriteFile {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("data.txt");
fw.write("Welcome to iKeySkills File Handling Tutorial.");
fw.close();
System.out.println("Data written successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Data written successfully.
3. Reading From a File using FileReader
import java.io.*;
public class ReadFile {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("data.txt");
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Welcome to iKeySkills File Handling Tutorial.
4. Appending Data to File
import java.io.*;
public class AppendFile {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("data.txt", true);
fw.write("\nThis line is appended.");
fw.close();
System.out.println("Data appended successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Data appended successfully.
5. Using BufferedWriter & BufferedReader (Fast IO)
BufferedWriter Example:
import java.io.*;
public class BufferedWrite {
public static void main(String[] args) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("notes.txt"));
bw.write("BufferedWriter writes fast!");
bw.close();
System.out.println("Data written.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
BufferedReader Example:
import java.io.*;
public class BufferedRead {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("notes.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
6. Using FileInputStream & FileOutputStream (Binary Files)
Used for images, PDFs, audio, videos.
Example:
import java.io.*;
public class BinaryCopy {
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("image.jpg");
FileOutputStream fout = new FileOutputStream("copy.jpg");
int b;
while ((b = fin.read()) != -1) {
fout.write(b);
}
fin.close();
fout.close();
System.out.println("Image copied successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
7. Reading a File Using Scanner
import java.io.*;
import java.util.*;
public class ScannerRead {
public static void main(String[] args) throws Exception {
File f = new File("data.txt");
Scanner sc = new Scanner(f);
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
sc.close();
}
}
8. Listing Files in a Folder
import java.io.*;
public class ListFiles {
public static void main(String[] args) {
File folder = new File("C:/JavaProjects");
String[] files = folder.list();
for (String f : files) {
System.out.println(f);
}
}
}
9. Delete a File
import java.io.*;
public class DeleteFile {
public static void main(String[] args) {
File f = new File("test.txt");
if (f.delete()) {
System.out.println("File deleted.");
} else {
System.out.println("Unable to delete.");
}
}
}
10. Java NIO (Modern File Handling)
java.nio.file provides faster, more powerful file operations.
Example:
import java.nio.file.*;
public class NIOExample {
public static void main(String[] args) throws Exception {
Path p = Paths.get("niofile.txt");
Files.write(p, "NIO writing example".getBytes());
String content = Files.readString(p);
System.out.println(content);
}
}
Real-Life Application Example (Log File System)
import java.io.*;
import java.time.*;
public class Logger {
static void log(String message) throws Exception {
BufferedWriter bw = new BufferedWriter(new FileWriter("log.txt", true));
bw.write(LocalDateTime.now() + " : " + message);
bw.newLine();
bw.close();
}
public static void main(String[] args) throws Exception {
log("User logged in");
log("User purchased a course");
}
}
Summary
-
Java File Handling allows reading/writing files on disk
-
File → create/delete files
-
FileWriter, FileReader → text files
-
BufferedReader, BufferedWriter → fast processing
-
FileInputStream, FileOutputStream → binary files
-
Scanner → simple file reading
-
Files + Paths (NIO) → modern and powerful
Keep practicing — you're doing amazing!
Happy Coding! ![]()