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
after clear
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:
| Return type | Method | Description |
| static ByteBuffer | allocate( int capacity) | The allocate() method create a byte buffer of specified capacity. |
import java.io.*;
|
| C:\>java ByteBufferClear Before clear method data in buffer import java.nio.ByteBuffer; import java.nio.channels.F After clear method data in buffer import java.io.*; |
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.