- Java NIO - Discussion
- Java NIO - Useful Resources
- Java NIO - Quick Guide
- Java NIO - FileLock
- Java NIO - CharSet
- Java NIO - AsynchronousFileChannel
- Java NIO - File
- Java NIO - Path
- Java NIO - Pipe
- Java NIO - Selector
- Java NIO - Buffer
- Java NIO - Gather
- Java NIO - Scatter
- Java NIO - Server Socket Channel
- Java NIO - Socket Channel
- Java NIO - DataGram Channel
- Java NIO - File Channel
- Java NIO - Channels
- Java NIO vs JAVA IO
- Java NIO - Environment Setup
- Java NIO - Overview
- Java NIO - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java NIO - File Channel
Description
As already mentioned FileChannel implementation of Java NIO channel is introduced to access meta data properties of the file including creation, modification, size etc.Along with this File Channels are multi threaded which again makes Java NIO more efficient than Java IO.
In general we can say that FileChannel is a channel that is connected to a file by which you can read data from a file, and write data to a file.Other important characteristic of FileChannel is this that it cannot be set into non-blocking mode and always runs in blocking mode.
We can t get file channel object directly, Object of file channel is obtained either by −
getChannel() − method on any either FileInputStream, FileOutputStream or RandomAccessFile.
open() − method of File channel which by default open the channel.
The object type of File channel depends on type of class called from object creation i.e if object is created by calpng getchannel method of FileInputStream then File channel is opened for reading and will throw NonWritableChannelException in case attempt to write to it.
Example
The following example shows the how to read and write data from Java NIO FileChannel.
Following example reads from a text file from C:/Test/temp.txt and prints the content to the console.
temp.txt
Hello World!
FileChannelDemo.java
import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.HashSet; import java.util.Set; pubpc class FileChannelDemo { pubpc static void main(String args[]) throws IOException { //append the content to existing file writeFileChannel(ByteBuffer.wrap("Welcome to TutorialsPoint".getBytes())); //read the file readFileChannel(); } pubpc static void readFileChannel() throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile("C:/Test/temp.txt", "rw"); FileChannel fileChannel = randomAccessFile.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(512); Charset charset = Charset.forName("US-ASCII"); while (fileChannel.read(byteBuffer) > 0) { byteBuffer.rewind(); System.out.print(charset.decode(byteBuffer)); byteBuffer.fpp(); } fileChannel.close(); randomAccessFile.close(); } pubpc static void writeFileChannel(ByteBuffer byteBuffer)throws IOException { Set<StandardOpenOption> options = new HashSet<>(); options.add(StandardOpenOption.CREATE); options.add(StandardOpenOption.APPEND); Path path = Paths.get("C:/Test/temp.txt"); FileChannel fileChannel = FileChannel.open(path, options); fileChannel.write(byteBuffer); fileChannel.close(); } }
Output
Hello World! Welcome to TutorialsPointAdvertisements