CharsetEncoderTest.java

import java.io.*;
import java.nio.*;
import java.nio.charset.*;
public class CharsetEncoderTest {
  public static void main(String[] args) throws IOException {
    Charset charset = Charset.forName("ASCII");
    CharsetEncoder encoder = charset.newEncoder();
    String st = "Bonjour Monde !";
    CharBuffer charB = CharBuffer.wrap(st);
    ByteBuffer byteB = encoder.encode(charB);
    System.out.println(st.length());      // Affiche 15
    System.out.println(byteB.position()); // Affiche 0
    System.out.println(byteB.limit());    // Affiche 15
    System.out.write(byteB.get());        // Affiche B
    System.out.write(byteB.get());        // Affiche o
    System.out.flush();
  }
}