Java NIO & Java IO - Comparative (Brief) Analysis
Executive Summary
Due to some reasons I ended up suggesting replacing an earlier code written using java.io.* api package with code written using java.nio.* api package - which was introduced to Java Community with the release of JDK v1.3.0
Using this information gained while trawling the web, I thought to pen down few pointers to help the community in show casing major differences between the 2 packages and few scenario's of when to use which package.
What this page is not
1. Suggestion to replace all implementations of java.io.* with java.nio.*
2. By any chance a "How to" primer on java.nio.* API
3. By any chance a "How to" primer on java.io.* API
4. Suggestion that java.nio.* is superior since it was introduced later and hence always use java.nio.*
Key Differences (IO vs NIO)
1. Classes and Interfaces within java.io package are stream based where those in java.nio package are buffer & channel based.
a. Within java.io.*, contents are read from /written to on a byte by byte basis until the data stream is empty.
b. Within java.nio.*, contents are read from / written to within a buffer as and when data is available within the channel.
c. Content as part of java.io package is not stored anywhere for review in future. Action is to be taken right away as the content is read otherwise necessary caching mecanism's need to be looked at.
d. Caching is built in as part of java.nio package since content is always read within / written from a buffer.
2. Classes and Interfaces within java.io package perform blocking IO where as those in java.nio package peform non-blocking IO.
3.
To be continued......
Due to some reasons I ended up suggesting replacing an earlier code written using java.io.* api package with code written using java.nio.* api package - which was introduced to Java Community with the release of JDK v1.3.0
Using this information gained while trawling the web, I thought to pen down few pointers to help the community in show casing major differences between the 2 packages and few scenario's of when to use which package.
What this page is not
1. Suggestion to replace all implementations of java.io.* with java.nio.*
2. By any chance a "How to" primer on java.nio.* API
3. By any chance a "How to" primer on java.io.* API
4. Suggestion that java.nio.* is superior since it was introduced later and hence always use java.nio.*
Key Differences (IO vs NIO)
1. Classes and Interfaces within java.io package are stream based where those in java.nio package are buffer & channel based.
a. Within java.io.*, contents are read from /written to on a byte by byte basis until the data stream is empty.
b. Within java.nio.*, contents are read from / written to within a buffer as and when data is available within the channel.
c. Content as part of java.io package is not stored anywhere for review in future. Action is to be taken right away as the content is read otherwise necessary caching mecanism's need to be looked at.
d. Caching is built in as part of java.nio package since content is always read within / written from a buffer.
2. Classes and Interfaces within java.io package perform blocking IO where as those in java.nio package peform non-blocking IO.
3.
To be continued......