Overview
New IO, the new IO introduced from java1.4 version, can be replaced Standard IO.
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.
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.
Allows one thread to monitor multiple channels.
A file operation object that supports both reading, writing and random access, equivalent to an array containing all bytes of the file .
IO is in bytes Or characters are the basic unit, and NIO uses data blocks as the basic unit.
IO operations block threads, NIO does not block threads.
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.
IO does not support locks, NIO supports locks, and the function of locks is to control access to files.
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.
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.
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.
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.
ByteBuffer byteBuffer=ByteBuffer.allocate();//通过分配指定长度的存储单位来创建缓冲区ByteBuffer byteBuffer=ByteBuffer.wrap(byte[] array);//基于数组创建缓冲区
put(Object data):将数据写入当前位置,同时将光标向前移动一个数据单位。
get():获取当前位置的数据,同时将光标向前移动一个数据单位。
flip():通过调整position与limit的值切换读写模式。
clear():并非清空缓冲区,而是调整position=0,limit=capacity,mark=-1。
array():将ByteBuffer中的数据复制的数组中。
在系统当中建立文件的映射,如果采用读写模式,那么对该映射的操作会反映到文件中。
由于将文件映射到内存中,资源消耗较大,只有在文件较大的情况下才将文件映射到内存中。
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!