Java Reference
In-Depth Information
7\Projects\MoveAndCopyFiles\classes\junkDirBackup
Starting move files...
MoveAndCopyFiles.jcp moved.
MoveAndCopyFiles.jcu moved.
MoveAndCopyFiles.jcw moved.
src_moveandcopyfiles.txt moved.
You can look at the directory to verify that the move has worked.
Press Enter to continue.
Deleting D:\Beginning Java SE
7\Projects\MoveAndCopyFiles\classes\junkDir...
Deleting files from
D:\Beginning Java SE
7\Projects\MoveAndCopyFiles\classes\junkDirBackup...
MoveAndCopyFiles.jcp deleted.
MoveAndCopyFiles.jcu deleted.
MoveAndCopyFiles.jcw deleted.
src_moveandcopyfiles.txt deleted.
Deleting D:\Beginning Java SE
7\Projects\MoveAndCopyFiles\classes\junkDirBackup...
After execution finishes successfully, the directories and files that are created by the program will all
have been deleted.
How It Works
The main() method first creates a new subdirectory, junkDir , in the current directory by calling the
createSingleDirectory() method. All the files in the current directory are then copied to junkDir
by using the copyFiles() method. The program then pauses so you can inspect your file system to verify
that the copy has indeed been completed.
Another subdirectory to the current directory, junkDirBackup , is created, and all the files in junkDir are
moved to junkDirBackup . The program pauses once more to enable you to verify the move.
Finally the main() method deletes junkDir , deletes the files from junkDirBackup , and then deletes
junkDirBackup .
Walking a File Tree
The java.nio.file.FileVisitor<T> interface specifies methods that you can use to walk through a
tree of directories and files. T is the type of file reference, usually type Path . The
java.nio.file.SimpleFileVisitor<T> class implements the methods in the FileVisitor<T> interface
that simply visits all files in a tree and rethrows any I/O exceptions that occur. You can extend the Sim-
pleFileVisitor<T> class to override the FileVisitor<T> methods to do what you want.
The methods that are declared in the FileVisitor<Path> interface are called by another method that you
call to initiate the process of walking the file tree; this is the static walkFileTree() method that is defined
in the Files class. This then calls the FileVisitor<Path> interface methods, which are presented in Table
9-7 .
TABLE 9-7 : FileVisitor<Path> Interface Methods
 
 
Search WWH ::




Custom Search