Summary and comparison of Java IO and NiO

Source: Internet
Author: User

I. Summary of IO flow

1, Java I/O mainly includes the following 3 levels:

Flow section-the most important part. such as: OutputStream, InputStream, Writer, reader, etc.
Non-streaming parts-such as: File class, Randomaccessfile class, and FileDescriptor class
Other--security-related classes in the file reading section, such as the Serializablepermission class, and the class of the file system associated with the local operating system, such as: FileSystem class and Win32filesystem class and Winntfilesystem class.

  

2. Different classification of flow-type parts

    • Flow direction by data: input stream, output stream
    • The format of the data object you want to manipulate: byte stream, character stream
    • by Source and whereabouts:
      • File: FileInputStream, FileOutputStream, FileReader, FileWriter
      • Byte[]:bytearrayinputstream, Bytearrayoutputstream

      • Char[]: CharArrayReader, Chararraywriterstring:stringbufferinputstream, StringReader, StringWriter
      • Network data flow: InputStream, OutputStream, Reader, Writer

These are also factors to consider when choosing to use IO streams, and there are three additional points to consider:

1. Do I need to convert the stream?
Yes, use the transform stream to convert from stream to reader, Writer:inputstreamreader.

2, whether the need for buffer to improve efficiency

Yes, plus buffered:bufferedinputstream, Bufferedouputstream, BufferedReader, BufferedWriter.

3. If you need to format the output

Ii. Summary of NiO

Three core APIs for Java NIO:

    • Channel (channels-Interface)
    • Buffers (buffer-abstract class)
    • Selector (Selector-abstract class)

Iii. options for IO and NiO

Compare the main differences between IO and NIO:

Io Nio
Stream oriented Buffer oriented
Blocking IO Non-blocking IO
No Selector Selector

1. Flow-oriented and buffer-oriented

The Java io-oriented stream means that one or more bytes are read from the stream every time, until all bytes are read, and they are not being slowed anywhere. In addition, it cannot move data in the stream back and forth. If you need to move the data read from the stream before and after it, you need to cache it to a buffer first.

Java NiO has a slightly different buffer-oriented approach. The data is read to a buffer that it processes later, and can be moved back and forth in the buffer if needed. This increases the flexibility of the process. However, you also need to check if the buffer contains all the data that you need to process. Also, make sure that when more data is read into the buffer, do not overwrite the data that has not been processed in the buffer.

2, blocking and non-blocking

The various streams of Java Io are blocked. This means that when a thread calls read () or write (), the thread is blocked until some data is read, or the data is fully written. The thread can no longer do anything during this time.

The non-blocking mode of Java NIO enables a thread to send requests to read data from a channel, but it can only get the data that is currently available and will not get anything if no data is available. Instead of keeping the thread blocked, the thread can continue to do other things until the data becomes readable. The same is true for non-blocking writes.

A thread requests to write some data to a channel, but does not have to wait for it to be fully written, and the thread can do something else at the same time. Threads typically use non-blocking IO idle time to perform IO operations on other channels, so a single thread can now manage multiple input and output channels (channel).

3. About selectors

The Java NiO selector allows a single thread to monitor multiple input channels, you can register multiple channels using a selector, and then use a separate thread to "select" the channel: these channels already have inputs that can be processed, or select the channels that are ready to be written. This selection mechanism makes it easy for a single thread to manage multiple channels.

Iv. how NiO and Io affect the design of applications

Whether you choose IO or the NIO toolbox, you may have an impact on the following aspects of your application design:

    • API calls to the NIO or IO classes.
    • Data processing.
    • The number of threads used to process the data.

Reference article: http://www.importnew.com/19046.html

  

  

Summary and comparison of Java IO and NiO

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.