Home  >  Article  >  Java  >  Overview of NIO and comparison between NIO and IO

Overview of NIO and comparison between NIO and IO

零下一度
零下一度Original
2017-06-27 09:47:491651browse

Overview

1.NIO

New IO, the new IO introduced from java1.4 version, can be replaced Standard IO.

2.Channel

Represents an open connection with the data source, which can read and write data. This kind of read and write operations are based on data piece.

3.Buffer

The buffer for channel to read and write data. The channel stores the read data into the Buffer and stores the data in the Buffer. Data is written to the file.

4.Selector

Allows one thread to monitor multiple channels.

5.RandomAccessFile

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

Comparison between NIO and IO

1. Basic data unit

IO is in bytes Or characters are the basic unit, and NIO uses data blocks as the basic unit.

2. Blocking

IO operations block threads, NIO does not block threads.

3. Index

There is no index for the IO operation stream, and the operation position cannot be specified. There are indexes in NIO data, and the operation location can be specified.

4. Lock

IO does not support locks, NIO supports locks, and the function of locks is to control access to files.

Three Channel

1. Object creation:

Create objects 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. Create based on the input stream Objects created based on the output stream can only be input, objects created based on the output stream can only be output, and objects created based on RandomAccessFile can be both input and output.

3. The data in the Channel has an index, and the read and write locations can be specified.

4. Commonly used methods

  • truncate(long size): intercept the content of the specified byte length from the beginning, Delete the rest. If the length is greater than the current size of the file, this operation fails.

Four Buffer

NIO provides a buffer for each basic data type, and the manipulation method is the same. Let's take ByteBuffer as an example to sort out.

1. Three important attributes

  • position: The current cursor position, used to determine the starting point of reading and writing.

  • #limit: upper limit, used to determine the range of reading and writing, excluding the index.

  • #capacity: Capacity, used to determine the maximum amount of data that can be stored.

2. Object creation

ByteBuffer byteBuffer=ByteBuffer.allocate();//通过分配指定长度的存储单位来创建缓冲区ByteBuffer byteBuffer=ByteBuffer.wrap(byte[] array);//基于数组创建缓冲区

3.常用方法:

  • put(Object data):将数据写入当前位置,同时将光标向前移动一个数据单位。

  • get():获取当前位置的数据,同时将光标向前移动一个数据单位。

  • flip():通过调整position与limit的值切换读写模式。

  • clear():并非清空缓冲区,而是调整position=0,limit=capacity,mark=-1。

  • array():将ByteBuffer中的数据复制的数组中。

五 MappedByteBuffer

在系统当中建立文件的映射,如果采用读写模式,那么对该映射的操作会反映到文件中。

由于将文件映射到内存中,资源消耗较大,只有在文件较大的情况下才将文件映射到内存中。

获取对象

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

有3中映射方式:

  • READ_ONLY:映射内容只允许读,不允许修改。

  • READ_WRITE:映射内容既允许读,也允许改,修改内容会反映到文件中。

  • PRIVATE:在本地创建一个副本,读写操作都是针对副本,写不会反映到原始文件中。

The above is the detailed content of Overview of NIO and comparison between NIO and IO. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn