Java IO Tutorial - Java Charset.encode(String str)








Syntax

Charset.encode(String str) has the following syntax.

public final ByteBuffer encode(String str)

Example

In the following code shows how to use Charset.encode(String str) method.

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
/*from  w  ww .  j a  va2 s. c  om*/
public class Main {

  public static void print(ByteBuffer bb) {
    while (bb.hasRemaining())
      System.out.print(bb.get() + " ");
    System.out.println();
    bb.rewind();
  }

  public static void main(String[] args) {
    Charset csets = Charset.forName("UTF-16LE");

    System.out.println(csets.name() + ":");
    print(csets.encode("java2s.com"));


  }
}

The code above generates the following result.