Java NiO Overview

Source: Internet
Author: User

Java NiO OverviewJava NIO full name java new IO,Is the API provided in jdk1.4.




The most basic differences between Java NiO and IO

1. Reading and Writing methodsIo is stream-oriented and can only be moved in one direction, one input stream (inputstream) an output stream (outputstream), one or more bytes from the stream, until the total byte is Read.


NIO is Buffer-oriented. Can be bidirectional, whether read or write, the data will be placed first into a buffer

2. Clogging and non-cloggingThe IO stream is blocked when a thread calls read () or write () until it has been read or write completed during this Time. The thread is in wait-blocking mode and does nothing else.


Nio is Non-clogging. When a thread sends a read/write request from the channel, the data is first placed in the Buffer. There is no need to wait for all reading and writing to complete during this period. This thread is capable of doing other things, usually running IO operations on other channels at spare time, and one thread can manage multiple channels


NIO Core Concept:Buffer (buffers), Channel (channels), Selector (selector)

Buffer

As mentioned earlier,Nio is Buffer-oriented. All read and write must first be placed in the Buffer.

All of the buffer types are inherited from the abstract class buffer, most often using bytebuffer, the Java base class basically has the corresponding buffer class to one by one accordingly. Class inheritance diagrams such as the Following:



Channel

Channel and IO Flow similar, The main difference is: the channel is bidirectional, readable writable, io flow is unidirectional. Can read or write only, and the operation channel does not write or read data directly from the Channel. is a channel that puts data into a buffer.
Most frequently used Channels:
FileChannel read/write File Data
Socketchannel socket Read-write data for TCP protocol
Datagramchannel UDP protocol Read and write data
The following is the process of sending a read or write request from the Client.


Simple example of reading and writing data into a file with filechannel

Read File Contents:

public static void main (string[] Args) {fileinputstream fin = null;try {fin = new FileInputStream ("c:\\nio.txt");} catch ( FileNotFoundException E) {e.printstacktrace ();} Get the Channel FileChannel FC = Fin.getchannel ();//create buffer bytebuffer = bytebuffer.allocate (1024);//read data to buffer try {fc.read ( buffer);} Catch (ioexception e) {e.printstacktrace ();} Buffer.flip (); while (buffer.remaining () > 0) {byte b = Buffer.get (); System.out.print (((char) b)); Try {fin.close ();} catch (ioexception e) {e.printstacktrace ();}}


Write File Contents:

public static void main (string[] Args) {file file = new file ("c:\\nio.txt"); FileOutputStream OutputStream = null;try {outputstream = new FileOutputStream (file);} catch (filenotfoundexception e1) {e 1.printStackTrace ();} FileChannel channel = Outputstream.getchannel (); Bytebuffer bufferwrite = bytebuffer.allocate (1024); String string = "hello NiO ..."; Bufferwrite.put (string.getbytes ()); You must call flip () to read the data to buffer First. The data is then read from the Buffer. Bufferwrite.flip (); try {channel.write (bufferwrite); channel.close (); outputstream.close ();} catch (ioexception e1) { E1.printstacktrace ();} }

Select
Select detects the read and write requests on all channels of the register, reads and writes when requested, and a thread manages multiple channels. Avoids the overhead of multi-threaded switching. There is no need to maintain multiple threads, such as

The differences between Javaio and NiO are described here, as well as the main concepts of nio. There are simple examples of NiO reading and writing file data, and the NIO does not go into it.

Interested students can see Jakob Jenkov series of Articles. Http://tutorials.jenkov.com/java-nio/index.html

The next step is to share some of the lessons learned from the NIO framework netty, as well as the use of Netty in the actual project Architecture.




Java NiO Overview

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.