AsynchronousFileChannel in Java NIO
Last Updated :
15 Dec, 2021
Java.nio package was introduced in the version java 1.4 edition. It allows us to deal with different channels concurrently because it supports concurrency and multi-threading. The asynchronous file channel API is responsible for the java NIO package and it is defined under the NIO channels package. In order to import the AsynchronousFileChannel API to our program follow the below syntax as follows:
Syntax:
import java.nio.channels.AsynchronousFileChannel
Asynchronous channels are safe for use by multiple concurrent threads because this channel enables file operations to execute asynchronously unlike synchronous I/O operations in which a thread enters into action and waits until the request is completed. This is the only difference between the asynchronousFileChannel and NIO's FileChannel.
The request is first passed by the threads to the kernel of the operating system to get it done while the thread continues to another job. After the job of the kernel is done, it signals the thread, then the thread acknowledges the signal and interrupts the current job and processes the I/O job as needed in asynchronous channels.
Approaches:
There are two approaches for achieving concurrency and these are as listed below. We will see the above two approaches in detail with the examples.
- Future Object
- Completion Handler
Approach 1: Future Object
It returns a java.util.concurrent.Future object. There are two useful methods to retrieve information and these two methods are -
- get() method: It returns the status of the operation that is handled asynchronously on the basis of which further execution of other tasks could get decided.
- isDone() method: This method will check whether the task is completed or not.
Example
Java
// Java Program to Illustrate AsynchronousFileChannel Class
// Via Future Object Approach
// Importing package
package com.java.nio;
// Importing required classes from java.nio package
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
// Main class
// FutureObjectExample
public class GFG {
// Method 1
// Main driver method
public static void main(String[] args) throws Exception
{
// Calling the readFile() method that will run first
readFile();
}
// Method 2
// To read the file
private static void readFile()
throws IOException, InterruptedException,
ExecutionException
{
// Path of the file
String filePath
= "C:/users/Sir/Desktop/fileCopy.txt";
// First create the file and then specify its
// correct path
printFileContents(filePath);
Path path = Paths.get(filePath);
AsynchronousFileChannel channel
= AsynchronousFileChannel.open(
path, StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(400);
Future<Integer> result = channel.read(buffer, 0);
// Checking whether the task is been completed or
// not
while (!result.isDone()) {
System.out.println(
"The process of reading file is in progress asynchronously.");
}
// Print and display statements
System.out.println("Is the reading done? "
+ result.isDone());
System.out.println(
"The number of bytes read from file is "
+ result.get());
buffer.flip();
System.out.print("Buffer contents: ");
while (buffer.hasRemaining()) {
System.out.print((char)buffer.get());
}
System.out.println(" ");
// Closing the channels using close() method
buffer.clear();
channel.close();
}
// Method 3
// To print the contents of the file
private static void printFileContents(String path)
throws IOException
{
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
String textRead = br.readLine();
System.out.println("Content in the File: ");
while (textRead != null) {
// After reading all the text from the file it
// print the number of bytes in the file.
System.out.println(" " + textRead);
textRead = br.readLine();
}
// Closing the channels
// Closing the fr object
fr.close();
// Closing the br object
br.close();
}
}
Output:
Approach 2: Completion handler
For this approach, we are going to use the CompletionHandler interface and It consists of two useful methods that we are going to override. In this, a completion handler is created for consuming the result of an asynchronous I/O operation as once a task is completed then only the handler has functions that are executed.
These two methods are as follows:
- completed() method: This method is invoked when the I/O operation completes successfully.
- failed() method: This method is invoked if the I/O operations fail.
Example
Java
// Java Program to Illustrate AsynchronousFileChannel Class
// Via Completion handler Approach
// Importing required classes from respective packages
package com.java.nio;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
// Main class
// CompletionHandler
public class GFG {
// Method 1
// Main driver method
public static void main(String[] args) throws Exception
{
// Calling the writefile() method
writeFile();
}
// Method 2
// To write into a file
private static void writeFile() throws IOException
{
// Custom string
String input = "Content to be written to the file.";
System.out.println("Input string: " + input);
byte[] byteArray = input.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
// Specifying path of File
Path path = Paths.get(
"C:/users/Sir/Desktop/fileCopy.txt");
AsynchronousFileChannel channel
= AsynchronousFileChannel.open(
path, StandardOpenOption
.WRITE); // calling the API
CompletionHandler handler
= new CompletionHandler() {
// Method 3
@Override
public void completed(Object result,
Object attachment)
{
System.out.println(
attachment + " completed and "
+ result + " bytes are written.");
}
// Method 4
@Override
public void failed(Throwable exc,
Object attachment)
{
System.out.println(
attachment
+ " failed with exception:");
exc.printStackTrace();
}
};
channel.write(buffer, 0, "Async Task", handler);
// Closing the channel object that we created earlier
// using close() method
channel.close();
printFileContents(path.toString());
}
// Method 5
// To print the file contents
private static void printFileContents(String path)
throws IOException
{
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
String textRead = br.readLine();
System.out.println("File contents: ");
// Till there is some content in file
while (textRead != null) {
System.out.println(" " + textRead);
textRead = br.readLine();
}
// Closing the fr object
fr.close();
// Closing the br object
br.close();
}
}
Output:
Similar Reads
java.nio.channels.spi.AsynchronousChannelProvider Class in Java
Java programming offers a crucial component known as the java.nio.channels.spi.AsynchronousChannelProvider class, which plays an indispensable role in managing asynchronous I/O operations. This particular class is an integral part of the java.nio.channels package and serves as a provider for channel
3 min read
Asynchronous and Synchronous Callbacks in Java
A CallBack Function is a function that is passed into another function as an argument and is expected to execute after some kind of event. The purpose of the callback function is to inform a class Sync/Async if some work in another class is done. This is very useful when working with Asynchronous ta
4 min read
File Handling in Java
In Java, with the help of File Class, we can work with files. This File Class is inside the java.io package. The File class can be used to create an object of the class and then specifying the name of the file.Why File Handling is Required?File Handling is an integral part of any programming languag
6 min read
java.nio.file.FileStore Class in Java
Java.nio.file is a package in java that consists of the FileStore class. FileStore class is a class that provides methods for the purpose of performing some operations on file store. FileStore is a class that extends Object from java.lang package. And few methods the FileStore class can inherit from
4 min read
java.nio.file.Paths Class in Java
java.nio.file.Paths class contains static methods for converting path string or URI into Path. Class declaration : public final class Paths extends ObjectMethods: MethodDescriptionget(String first, String... more) This method converts a path string, or a sequence of strings that when joined form a p
2 min read
java.nio.file.FileSystem class in java
java.nio.file.FileSystem class provides an interface to a file system. The file system acts as a factory for creating different objects like Path, PathMatcher, UserPrincipalLookupService, and WatchService. This object help to access the files and other objects in the file system. Syntax: Class decla
4 min read
java.nio.FloatBuffer Class in Java
A Buffer object can be termed as a container for a fixed amount of data. The buffer acts as a storing box, or temporary staging area, where data can be stored and later retrieved according to the usage. Java Buffer classes are the foundation or base upon which java.nio is built. Float buffers are cr
4 min read
FileChannel Class tryLock() Method in Java with Examples
In the case of a multi-threaded program, where multiple threads are in execution concurrently, we require locks to be acquired and released to maintain synchronization among the processes. FileChannel Class of java also provides a method known as trylock() which is used to acquire a lock on the File
4 min read
java.nio.Buffer Class in Java
The Buffer class provides a buffer or a container for data chunks of specific primitive types. A finite sequence of elements is stored linearly in a buffer. Important properties of a buffer that make it convenient to perform read and write operations in the data are: Capacity: This property determin
4 min read
java.nio.file.attribute.AclEntry Class in Java
ACL entries are Examined by this class is validating on the ACL model declared in RFC 3530: Network File System i.e.(NFS) version of 4th Protocol and having four components within it as follows characteristics as follows:Â This type of component are determining if the entry grants or denies its acce
3 min read