CharBuffer

CharBuffer is part of the java.nio package and is a buffer for handling characters. It is useful for character-based input/output operations, such as text parsing, network communication, or file processing. It works similarly to ByteBuffer but specifically stores 16-bit char values.

Commonly Used Methods

Simple Program using CharBuffer

Writing and Reading Characters

import java.nio.CharBuffer;

public class SimpleCharBufferDemo {
    public static void main(String[] args) {
        // Create CharBuffer with capacity 10
        CharBuffer buffer = CharBuffer.allocate(10);

        // Put characters
        buffer.put('H');
        buffer.put('e');
        buffer.put('l');
        buffer.put('l');
        buffer.put('o');

        // Flip to switch to read mode
        buffer.flip();

        System.out.println("Reading characters from buffer:");
        while (buffer.hasRemaining()) {
            System.out.print(buffer.get());
        }
    }
}

Problem Statement:

LotusJavaPrince is developing a text-processing application. Mahesh challenges him to implement a filter that reads characters from a CharBuffer, removes vowels, and rewrites the remaining characters. The application should:

  • Use CharBuffer for both input and output
  • Process each character one by one
  • Demonstrate usage of key buffer methods: get(), put(), flip(), clear(), rewind()
import java.nio.CharBuffer;

public class CharBufferCaseStudy {
    public static void main(String[] args) {
        String input = "Welcome Mahesh and LotusJavaPrince!";
        System.out.println("Original: " + input);

        // Wrap input string in CharBuffer
        CharBuffer inputBuffer = CharBuffer.wrap(input);
        CharBuffer outputBuffer = CharBuffer.allocate(input.length());

        while (inputBuffer.hasRemaining()) {
            char ch = inputBuffer.get();
            if (!isVowel(ch)) {
                outputBuffer.put(ch);  // Write only consonants
            }
        }

        // Prepare output buffer for reading
        outputBuffer.flip();

        // Convert to string
        StringBuilder result = new StringBuilder();
        while (outputBuffer.hasRemaining()) {
            result.append(outputBuffer.get());
        }

        System.out.println("Without vowels: " + result.toString());
    }

    // Helper method to check for vowels
    private static boolean isVowel(char ch) {
        return "AEIOUaeiou".indexOf(ch) != -1;
    }
}

Output:

Original: Welcome Mahesh and LotusJavaPrince!
Without vowels: Wlcm Mhsh nd LtsJvPrnc!

The CharBuffer class in Java NIO is a specialized buffer for handling sequences of characters efficiently. As part of the java.nio package, it provides a structured way to manage character data in memory, offering a more flexible and performant alternative to traditional character streams.

Built on top of the abstract Buffer class, CharBuffer maintains the core properties — position, limit, capacity, and mark — which give developers precise control over character read/write operations. It supports both read-only and read-write modes and allows slicing, duplicating, and compacting for complex data manipulation scenarios.

Scroll to Top