Home  >  Article  >  Java  >  What is the difference between io and nio in java

What is the difference between io and nio in java

PHPz
PHPzforward
2023-05-13 15:04:062712browse

1. The first difference between Java IO and NIO is that IO is stream-oriented and NIO is buffer-oriented.

Java IO is stream-oriented which means that one or more bytes can be read from the stream at a time. How many bytes to read from the stream is entirely up to the programmer. These bytes cannot be cached anywhere; that is, you cannot move data forward or backward from the stream. If you really need to move data, you need to cache the data in a buffer first.

Java NIO views Buffer slightly differently. When data is read into the buffer, the buffer will be processed later. You can move data around in the buffer if you need to; this gives the programmer more flexibility in processing. However, you must check that the buffer contains all the data you need to process. Additionally, you must ensure that when read data arrives in the buffer, you cannot overwrite unprocessed data.

Blocking and non-blocking IO

2. Various streams in Java IO are blocked. Java NIO is a non-blocking mode to ensure that threads require reading from the channel. Get data.

Various streams in Java IO are blocked, that is, when the read() method and write() method are called, the thread will be blocked until the data is read or completely written . During this time, the thread processing the stream will not be able to complete.

Java NIO non-blocking mode ensures that the thread requests to read data from the channel. The channel cannot be used until then, or if no data is available at this time, nothing is done. Instead of holding the block until the data can be read, here the thread can do other things.

What are the basic data types of java

The basic data types of Java are divided into:

1. Integer type, used to represent the data type of integer.

2. Floating point type, a data type used to represent decimals.

3. Character type. The keyword of character type is "char".

4. Boolean type is the basic data type that represents logical values.

The above is the detailed content of What is the difference between io and nio in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete