IO APIs in Java - NIO, NIO 2
Inputs and Outputs are the bread and butter of computer programming. You read from a hardware resource and you write to a hardware resource. Over the years there have been improvements on the Operative Systems on the hardware as to how IO should be managed.
Java has been a platform independent language that optimizes IO for the platform. The original IO api - java.io.* could not handle the change so the developers built the NIO API. New IO API provides -
Non blocking IO
Buffered IO (Bulk IO)
Selectors (manage multiple IO activities from single thread)
The NIO API was shipped with 1.4 JDK. Another update has been released to NIO and It's NIO 2 and it is shipped with Java 7. Below are the new features.
Asynchronous IO (Finally getting callbacks on IO operations)
Complete Socket-Channel facility (I am going through this)
Improvements to the file system (New java.io.File is java.nio.file.Path)
In this week I will be diving through the old NIO API and the newer NIO 2 APIs studying how things work and why the improvements matter to us. Teaser to some of the codes for your viewing pleasure -
import java.nio.file.*; Path home = Path.get("/home/gus"); Path profile = home.resolve(".bash_profile"); // Backup existing file profile.copyTo(home.resolve(".bash_profile.backup")); // Append useful stuff to the profile WritableByteChannel ch = profile.newSeekableByteChannel(APPEND); try { appendStuff(ch); } finally { ch.close(); }
















