Example of SeekableByteChannel in Java | java.nio.channels API

By Arvind Rai, November 29, 2013
java.nio.channels.SeekableByteChannel is NIO 2 API. SeekableByteChannel reads and write both. It has variable length related to the entity like file with which it is connected. It truncates or increases its size as needed. SeekableByteChannel has a current positing can be set by user. The method description of SeekableByteChannel are given below.

position() : returns the position of SeekableByteChannel in the file or any other entity.
position(long new) : It sets new position of SeekableByteChannel and start reading or writing from that position.
read(ByteBuffer dst) : reads a file into ByteBuffer.
size() : It returns the size of file which is SeekableByteChannel is connected..
truncate(long size) : It truncates the file with which SeekableByteChannel is connected.
write(ByteBuffer src) : Writes the byte array to the given Path.

How to instantiate SeekableByteChannel | Files.newByteChannel()

Files.newByteChannel returns the object of SeekableByteChannel . SeekableByteChannel opens a file to read or write and returns SeekableByteChannel. The syntax is given below.
public static SeekableByteChannel newByteChannel(Path path,   OpenOption...  opts)
 
Path is the location of file to be read or written. OpenOption is the file mode like READ, APPEND or write or more than one.

Read File Using java.nio.channels.SeekableByteChannel


SeekableByteChannelExample.java
package com.concretepage.io.file;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class SeekableByteChannelExample {
	public static void main(String[] args) throws IOException {
		 Path path = Paths.get("D:/cp","file1.txt");
		 SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.READ);
		 ByteBuffer bf = ByteBuffer.allocate(1024);
	         int i = 0;
		 while((i=sbc.read(bf))>0){
			bf.flip();
			System.out.println(i); 
			bf.clear();
			
		 }
	} 
}
 
File has been open in read mode using StandardOpenOption.READ. SeekableByteChannel is connected to given file by Path.

Write File Using java.nio.channels. SeekableByteChannel


SeekableByteChannelExample.java
package com.concretepage.io.file;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class SeekableByteChannelExample {
	public static void main(String[] args) throws IOException {
		 Path path = Paths.get("D:/cp","file.txt");
		 SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.APPEND);
		 String s= "Hellow World!";
		 ByteBuffer bfSrc = ByteBuffer.wrap(s.getBytes());
		 sbc.write(bfSrc);
		 sbc.close();
	} 
 
File has been open in append mode using StandardOpenOption.APPEND. In this mode SeekableByteChannel writes the byte sequence and append to the data if already exist.
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us