Home Tutorial Java Corejava Nio Use of putChar() method of ByteBuffer class.

 
 

Use of putChar() method of ByteBuffer class.
Posted on: July 29, 2010 at 12:00 AM
Advertisement
In this tutorial you will see the use of putChar() method of ByteBuffer class.

Use of putChar() method of ByteBuffer class.

 In this tutorial, we will see how to write the given character into byte buffer.

ByteBuffer API:

The java.nio.ByteBuffer class extends java.nio.Buffer class. It provides the following methods:

Return type Method Description
static ByteBuffer allocate( int capacity)  The allocate(..)method allocate a new byte buffer.
abstract ByteBuffer putChar(char value) The putChar(..) method write two byte containing the given character value into associated buffer.
abstract char getChar() The getChar() method read  2 byte from current position and increment position.

code

import java.nio.*;
import java.nio.ByteBuffer;

public class BytePutChar {
  public static void main(String[] args) {
    ByteBuffer bytebuf = ByteBuffer.allocate(256);
    bytebuf.putChar('B');
    bytebuf.putChar('h');
    bytebuf.putChar('a');
    bytebuf.putChar('r');
    bytebuf.putChar('a');
    bytebuf.putChar('t');
    System.out.println(bytebuf);
    bytebuf.flip();
System.out.println("Character data in byte buffer :");
    while (bytebuf.hasRemaining()) {
      System.out.print(bytebuf.getChar());
    }
  }
}

Output

:\>java BytePutChar
java.nio.HeapByteBuffer[pos=12 lim=256 cap=256]
Character data in byte buffer :
Bharat

Download this code

Advertisement

Related Tags for Use of putChar() method of ByteBuffer class.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 29, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com