Java CharBuffer convert to char array

Description

Java CharBuffer convert to char array


import java.nio.CharBuffer;
import java.util.Arrays;

public class Main {
  public static void main(String[] argv) throws Exception {
    char[] charArray = new char[] { 'a', 'b', 'c', 'd', 'e' };
    CharBuffer bb = CharBuffer.wrap(charArray);

    charArray = toArray(bb);// w w  w.ja  va  2  s  .  co m

    System.out.println(Arrays.toString(charArray));
  }

  public static char[] toArray(final CharBuffer buffer) {
    char[] array = new char[buffer.limit()];
    buffer.get(array);
    return array;
  }

}



PreviousNext

Related