NIO, javanio

Source: Internet
Author: User

NIO, javanio

 

1 Overview 1.NIO

New IO, a New IO introduced from java1.4, can replace standard IO.

2. Channel

Represents an open connection to the data source, which can read and write data. Such read and write operations are based on data blocks.

3. Buffer

The Buffer for reading and writing data by the channel. The channel stores the read data in the Buffer and writes the data in the Buffer to the file.

4. Selector

Allows a thread to monitor multiple channels.

5. RandomAccessFile

A file operation object that supports both reading and writing and random access is equivalent to an array containing all the bytes of the file.

Ii. Comparison between NIO and IO 1. Basic data unit

IO is based on bytes or characters, while NIO is based on data blocks.

2. Blocking

IO operations block threads, and NIO does not block threads.

3. Index

The stream operated by I/O does not have an index and the operation location cannot be specified. There is an index in the NIO data. You can specify the operation location.

4. Lock

IO does not support locks. NIO supports locks. The lock is used to control access to files.

3. Channel 1. Object creation:

Create an object based on input and output streams:

InputStream is=new FileInputStream(String name);FileChannel channel=is.getChannel();

Create an object based on the RandomAccessFile object

RandonAccessFile file=new RandomAccessFile(String name ,"rw");FileChannel channel=file.getChannel();

2. objects created based on the input stream can only be input, and objects created based on the output stream can only be output. objects created based on RandomAccessFile can be both input and output.

3. The data in the Channel has an index. You can specify the read/write location.

4. Common Methods
  • Truncate (long size): captures the content of the specified byte length from the beginning, and deletes the remaining parts. This operation fails if the length is greater than the current file size.
Four Buffer

NIO provides a buffer for each basic data type, and the operation method is the same. The following uses ByteBuffer as an example to describe it.

1. Three Important attributes
  • Position: The current cursor position, used to determine the start point of read/write.
  • Limit: the upper limit used to determine the read/write range, excluding the index.
  • Capacity: capacity, used to determine the maximum data size that can be stored.
2. Object Creation
ByteBuffer byteBuffer = ByteBuffer. allocate (); // create a buffer ByteBuffer byteBuffer ByteBuffer = ByteBuffer. wrap (byte [] array) by allocating storage units of the specified length; // create a buffer based on the array
3. Common Methods:
  • Put (Object data): writes data to the current position, and moves the cursor forward to a data unit.
  • Get (): get the data at the current position, and move the cursor forward to a data unit.
  • Flip (): Change the read/write mode by adjusting the value of position and limit.
  • Clear (): instead of clearing the buffer, adjust position = 0, limit = capacity, mark =-1.
  • Array (): copy the data in ByteBuffer to the array.
MappedByteBuffer

Create a file ing in the system. If the read/write mode is used, the ing operation will be reflected in the file.

Because the file is mapped to the memory, the resource consumption is large, and the file is mapped to the memory only when the file is large.

Get object
MappedByteBuffer mbb=channel.map(MapMode mode,long offset,long size);

There are 3 ing methods:

  • READ_ONLY: The ing content can only be read and cannot be modified.
  • READ_WRITE: The ing content can be read or modified. The modified content is reflected in the file.
  • PRIVATE: create a local copy, read/write operations are for the copy, the write will not be reflected in the original file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.