Home
Username:
Password:
Java 6 Tutorials

I/O / NIO




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

The letters NIO stand for New IO, it's purpose is not to replace the existing streaming IO it's meant to add to it, to make it easier to do some things that are difficult to do now. You can lock regions of a file and prohibit other programs from reading or writing until you release the lock. You can have the contents of a file appear in memory in your program so you can randomly read from it and write to it. You can read and write data as blocks instead of being required to organize your data into a stream of bytes. This program creates a file and performs a block write to it. You open the file by creating a stream object in this case a file output stream is used to create a file and to open it for writing. To be able to perform NIO operations you need to call the Get Channel Method to get a file channel object. The NIO block operations are performed by reading and writing the contents of byte buffer objects. This call to allocate create byte buffer of the specified size, in this example it's a 20 byte block. You can work directly with the bytes and the buffer or you can treat the allocate block as if it were an array of another type. In this case its to be address as an array of characters, but it could just as well be addressed as an array of shorts and floats or doubles. Each of these has its own set of put methods, this one has a put method that will write a string of Unicode characters to the internal block. The channel object can be used to change the position of the file to that of the next write. If this method specifies a location beyond the end of file as in this case the file will be extended so the write can be performed at this location. The write method writes the entire array as a block to the file at the specified position, then the channel is closed. Here's what happens when you run it. That should have constructed the file, this is a hex dump of the file that was created. Notice that the characters are all in full 16 bit Unicode encoding just the way they were in memory. This is not the UTF 8 encoding that you get from character streams. Also the first 20 bytes of the file were inserted as filler because of the file positioning. The API is symmetric in that you can read the file in the same way you wrote. You can create a file input stream, position the channel and read a block of bytes from the same location. One of the things that you can do within IO that you cannot do with standard streaming IO is map sections of the file into memory. To show you how this works this example opens a file for read-write access, the call to the Get Channel Method creates a file channel object. The map method is then used to get a buffer, in this case it's a special buffer called a map byte buffer. Now this is an example of a read-write map byte buffer, but you can have them that are read only or write only. This works like any other buffer except it is connected to the file that is the content of the buffer is always the same as that of the file. If another program makes a change to the file, this buffer in memory will also change. Also if this program makes a change in the buffer, the change is immediately written to the file. Now this buffer is built to contain the data at 20 bytes offset into the file for a length of 18 bytes and you can create a type of buffer of a different data type and you can access it that way. You then call load to initialize the buffer from the file, then you can read from the contents of the file by reading from the contents of the buffer and you can also write to it. If you put something into the buffer it will go all the way to the file, here's how it runs. You can see the character that was read from the file that we created earlier, a hex dump of the file shows that the contents of the file were changed by the program by only changing the contents of memory. There are a few other things that can be done with NIO, probably the most important is the ability to lock all or parts of a file. Streams will always be the more common form of IO but when you need to do something special you need to look into NIO. In the next lesson the course starts its wrap up by looking into some of the gotchas and traps that Java sets for you.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available