Home Tutorial Java Corejava Nio Compare two buffer's content

 
 

Compare two buffer's content
Posted on: July 24, 2010 at 12:00 AM
In this tutorial you will learn about how to compare two buffer's content

Compare two buffer's content

In this tutorial we will see how to create a buffer and put content into it and compare byte data of one buffer with another.

Code:

import java.nio.CharBuffer;

public class CharBufferDemo {
  public static void main(String[] args) {
    CharBuffer charbuffer1 = CharBuffer.allocate(6);
    CharBuffer charbuffer2 = CharBuffer.allocate(6);
    charbuffer1.put('B').put('U').put('F').put('F').put('E').put('R');
    charbuffer2.put('B').put('U').put('F').put('F').put('E').put('R');
    charbuffer1.rewind();
    charbuffer2.rewind();

    if (charbuffer1.limit(5).equals(charbuffer2.limit(5)))
      System.out.println("True");
    else
      System.out.println("False");
  }
 

Output:

  True

Download this code

Related Tags for Compare two buffer's content:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.