FileChannel
is a class in java.nio.channels
that provides a faster and more flexible way to read, write, map, and manipulate files. It works with ByteBuffer
and allows for operations like random access, memory-mapped I/O, and file locking.
Unlike InputStream
/OutputStream
, it supports non-blocking I/O and asynchronous operations.
Opening a FileChannel

Commonly Used Methods

Simple Program: Writing and Reading from a File
import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class FileChannelSimple { public static void main(String[] args) throws Exception { // Write to file RandomAccessFile file = new RandomAccessFile("data.txt", "rw"); FileChannel channel = file.getChannel(); String message = "Hello, FileChannel!"; ByteBuffer buffer = ByteBuffer.allocate(64); buffer.put(message.getBytes()); buffer.flip(); channel.write(buffer); // Read from file channel.position(0); buffer.clear(); channel.read(buffer); buffer.flip(); System.out.print("Read: "); while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); } channel.close(); file.close(); } }
Problem Statement
Mahesh is building a custom logging system for his app.He wants LotusJavaPrince to design a tool that:
- Accepts a log message from the user.
- Writes the log message with a timestamp to a file
log.txt
. - Prints the entire log file to the console afterward.
Mahesh insists this be implemented using FileChannel, not BufferedWriter.
import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class FileChannelLogger { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); System.out.print("Enter log message: "); String userLog = scanner.nextLine(); String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); String logEntry = timeStamp + " - " + userLog + "\n"; // Append the log to file RandomAccessFile file = new RandomAccessFile("log.txt", "rw"); FileChannel channel = file.getChannel(); channel.position(channel.size()); // move to end for appending ByteBuffer buffer = ByteBuffer.wrap(logEntry.getBytes()); channel.write(buffer); // Read and print the entire log file channel.position(0); buffer = ByteBuffer.allocate((int) channel.size()); channel.read(buffer); buffer.flip(); System.out.println("\n--- Log File Contents ---"); while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); } channel.close(); file.close(); scanner.close(); } }
Output
Enter log message: Mahesh logged in
--- Log File Contents ---
2025-05-31 10:22:18 - Mahesh logged in
Code language: CSS (css)
The FileChannel
class in Java NIO is a powerful and flexible tool for performing file I/O operations with enhanced performance and control. Unlike traditional FileInputStream
and FileOutputStream
, FileChannel
allows both reading and writing, supports random access, and works directly with buffers like ByteBuffer
.
It supports key features such as:
- Memory-mapped files using
map()
for extremely fast access to large files, - Positioned reads/writes for random-access file manipulation,
- Efficient data transfer between channels via
transferTo()
andtransferFrom()
, - Locking mechanisms using
lock()
andtryLock()
for safe file access in concurrent environments.
FileChannel
is especially beneficial for applications that require high-speed data handling, such as databases, file servers, media processing, and large-scale data manipulation.