What is the exact use of java nio package when already methods are available with io package

前端 未结 5 1182
清歌不尽
清歌不尽 2020-12-12 20:43

I was learning java nio package and I realized there are lots of methods already provided by File which nio.Files is providing again by using Path class. Like that few more

5条回答
  •  独厮守ぢ
    2020-12-12 21:08

    Java NIO: Channels and Buffers
    In the standard IO API you work with byte streams and character streams. In NIO you work with channels and buffers. Data is always read from a channel into a buffer, or written from a buffer to a channel.

    Java NIO: Non-blocking IO
    Java NIO enables you to do non-blocking IO. For instance, a thread can ask a channel to read data into a buffer. While the channel reads data into the buffer, the thread can do something else. Once data is read into the buffer, the thread can then continue processing it. The same is true for writing data to channels.

    Java NIO: Selectors
    Java NIO contains the concept of "selectors". A selector is an object that can monitor multiple channels for events (like: connection opened, data arrived etc.). Thus, a single thread can monitor multiple channels for data.
    More detail on orcale

提交回复
热议问题