The New Input/Output (NIO) API provides the java.nio and java.nio.channels packages for buffer management, advance I/O file system, and file locking mechanism. The java.nio package contains the ByteBuffer classes that allow you to store the contents of a text file. To read, write, and lock the text files, you can use the FileLock and FileChannel classes available in the java.nio.channels package.
This chapter explains how to develop a Text Editor application using the java.nio and java.nio.channels packages and create, edit, print, and save text files.
The Text Editor application allows you to read, write, print, and lock a specific text file. You can also change the color and font of the text file. The Text Editor application provides various editing and formatting features, such as cut, copy, paste, undo, redo, select all, find, and word wrap.
The Text Editor application uses the following files:
Editor.java : Creates a user interface that contains a menu bar and a text area. There are five menus on the menu bar: File, Edit, Format, Locks, and Help. Each menu contains several menu items. You can use these items to perform various tasks , such as opening, saving, or printing a file.
ActionPerform.java : Implements the commands to create, open , save, lock, or copy a file. It also implements commands to find a word in a file. In addition, this class also implements the commands to cut, copy, paste, undo, redo, set font, set text color, and insert date in the file.
FontClass.java : Creates a font dialog box that provides options to change the type, size , and style of the font in the text file.
ColorClass.java : Creates a color dialog box with sliders. You can use this dialog box to specify the Red Green Blue (RGB) value and change the color of the file text.
PrintClass : Opens a print dialog box that allows you to print a document. You can also use this dialog box to change the properties of the document, such as page layout and paper quality.
Help.java : Creates a help dialog box that lists the steps to use the Text Editor.
Figure 6-1 shows the architecture of the Text Editor application:
The Editor.java file calls ActionPerform.java to perform the file edit, format, and lock operations. After the ActionPerform.java file implements these operations, it returns the results to the Editor.java file. The Editor.java file then displays the results to the end user. The ActionPerform.java file calls the PrintClass.java file to print a file. To change the font type, size, and style of the text file, the ActionPerform.java file calls the FontClass.java file. The ActionPerform.java file calls the ColorClass.java file to change the color of the file text.
If an end user selects the help file option, the Editor.java file calls the ActionPerform.java file, which, in turn , calls the Help.java file. The Help.java file lists the steps required to use the Text Editor.