In this tutorial, we will discuss the use of clear() method. The Buffer class is a
container for handling primitive data type. The clear() method is available in Buffer class.
The ByteBuffer class is available in java.nio package.
In this example, the clear() method clean a buffer for further use, and
method the position will be zero and limit will be capacity of buffer. The method allocate
( int capacity) creates a new byte buffer. The FileChannel class creates a channel for reading,
and writing file. The FileChannel is similar to the stream but they are few different. The channel
can read write both, but stream can either read-only or write-only. The getChannel method of
FileInputStream class returns the channel associated with this file input stream. The read()
method of FileChannel class fills byte buffer.
About ByteBuffer API:
The java.nio.ByteBuffer class extends java.nio.Buffer class. It provides the following methods:
|static ByteBuffer||allocate( int capacity)||The allocate() method create a byte buffer of specified capacity.|
Before clear method data in buffer
After clear method data in buffer
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.