The New Input/Output (NIO) API supports the java.nio, java.nio.channels, and java.util.regex packages for buffer management, character conversion, and regular expression matching. The java.nio and java.nio.channels packages contain the File, FileChannel, and CharBuffer classes to read and buffer the contents of a file. The java.util.regex package provides the Pattern and Matcher classes to match regular expressions.
This chapter explains how to develop a File Search application, which uses the above-mentioned NIO packages to search for specific files.
The File Search application enables an end user to search for files that are available at a specific location and contain a specific text pattern. The application also searches for all the instances of the specified text pattern in the files and displays the start position of the pattern to end users.
The File Search application uses the following files:
Search.java : Creates a user interface that helps an end user specify the criteria to search for specific files and view the retrieved result
FileList.java : Searches for files that are available at the specified location and contain the specified text pattern
Help.java : Creates a window that lists the steps to use the File Search application
Figure 4-1 shows the architecture of the File Search application:
In the File Search application, the Search.java file calls the FileList.java file to search for files that are present at the specified location and contain the specified text pattern. The FileList.java file searches for these files and passes the search results to the Search.java file. The Search.java file then displays these results to the end user in a tabular format. Before starting the file search process, if an end user opts to view the application help file, the Search.java file calls the Help.java file to display the steps to use the File Search application.