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  w w  . ja va  2  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.





















Home »
  Java Tutorial »
    java.nio.charset »




Charset
CharsetDecoder
CharsetEncoder
StandardCharsets