nio : Java Glossary


Introduction Tips
Memory Mapped Files Books
Direct Buffers Learning More
Ordinary I/O Links


Even though I have written some code that at least functions using NIO (New Input/Output) I can’t say I understand it. I primarily just glue together methods based on the types of parameters and return types. I don’t have an overall picture of how it works or why it works, or what it is for, as I do for ordinary I/O. I am far from a reliable guide.

If you keep your data in buffers, off the Java heap and ignore most of it, NIO will work faster than ordinary I/O.

Introduced in Java 1.4, the java.nio NIO package allows I/O more like that available in other lower level languages like C. You can memory map files. You can read and write blocks of data direct from disk, rather than byte by byte. It deals with the endian problem when you fish the data out of the buffer, not during the read. You can do non-blocking asynchronous I/O. You can lock files or parts of files. Regular I/O is based on stacking the methods you need in a rather verbose and highly asymmetric way, in InputStream, Reader, BufferedReader. The nio design is cleaner, simpler and more efficient. You could do all your I/O with nio if you chose. Unfortunately, at this point the File I/O Amanuensis will not generate nio examples, however, there some examples in the ByteBuffer entry.

Memory Mapped Files

Memory mapped files chew up huge amounts of virtual RAM (Random Access Memory) and real RAM as well for the lookup tables and cached data, so they are not for routine use. They will mainly help random access especially when you have several threads accessing the same file. Don’t just assume they will improve performance. Further, they are limited by the address space. In a 32-bit JVM (Java Virtual Machine), you will be lucky if you can even handle a 1GB file window. Test both ways.

Direct Buffers

Direct buffers help primarily for bulk file operations, slewing through files doing little processing of the data or bulk copying. They are only for when you can allocate a mighty buffer at the start of the program and keep it till completion. Normal GC (Garbage Collection) does not recover the unused direct buffers. Again, test both ways. Also be aware performance ratios will differ on different platforms.

Ordinary I/O

Read and writing individual bytes goes through a lot of indirection, even though what you are trying to achieve is very simple. For Oracle’s HotSpot you need to make sure that the method with your inner loop is very small. If it isn’t then the access code won’t be in-lined and it will run slowly.


NIO is for use when you have a lot of data to process and you don’t have to fish all of it out of the buffer. In other times it can be slower, such as this example where we read a file an once the fish all the bytes out of the buffer to convert them to a String, not a good use of NIO.


book cover recommend book⇒Java NIOto book home
by Ron Hitchens 978-0-596-00288-6 paperback
publisher O’Reilly recommended
published 2002-08-15
Covers NIO (new I/O) that arrived with JDK 1.4.
Australian flag abe books anz abe Canadian flag
German flag abe Canadian flag
German flag Chapters Indigo Canadian flag
Spanish flag Chapters Indigo eBooks Canadian flag
Spanish flag abe American flag
French flag abe American flag
French flag Barnes & Noble American flag
Italian flag abe Nook at Barnes & Noble American flag
Italian flag Kobo American flag
India flag Google play American flag
UK flag abe O’Reilly Safari American flag
UK flag Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.

Learning More

Oracle’s Javadoc on nio overview package : available:
Oracle’s Javadoc on CharSet class : available:
Oracle’s Javadoc on CharSetDecoder class : available:
Oracle’s Javadoc on ByteBuffer class : available:
Oracle’s Javadoc on MappedByteBuffer class : available:
Oracle’s Javadoc on CharBuffer class : available:
Oracle’s Javadoc on FileChannel class : available:
Oracle’s Javadoc on SocketChannel class : available:
Oracle’s Javadoc on Pipe class : available:

This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number