The Java runtime environment provides access to some useful file management functions in the java.nio.file.Files class
and the java.io.File class. The Files class is what we will be using in this lesson. The File class is used mostly
for input stream and output stream situations. Also, Java provides a file dialog class for choosing files from
the file system called javax.swing.JFileChooser.
Here is an example with JFileChooser:
In the example provided above scroll down to "setCurrentDirectory". The parameter passed in is the file directory
that will be displayed in the dialog box when show is called. In this situation, we use "new File(".")" which
means use the directory that "java FileChooserExample" was executed in.
Scroll down a little farther until you see "show*" function. The parameter is the java.awt.Component parent
of the JFileChooser. In this situation, we used a Frame object that we never make visible. When you use
the JFileChooser in real applications it is wise to choose a Frame that is displayed on the computer
If the "intResp" isn't equal to JFileChooser.APPROVE_OPTION then the user chose to cancel the file selection
and we should exit the operation of displaying the file path.
The "getSelectedFile" function returns a java.io.File corresponding to the file that the user selected.
In the next example, we will copy CopyMe.txt from the current directory to
the directory "files" inside the current directory. So, before executing the example, download the
"CopyMe.txt" file to your current directory and create a folder called "files" in your current
Here is the example:
In the above example, scroll down to the Path declarations in the "main" function. We create the Path by using
the "getAbsolutePath" function of java.io.File . Next we use java.nio.file.Files static function "copy" to copy
the file at "pathCopySource" to the Path specified by "pathCopyTarget".
The java.nio.file.Files class also has a function called "move" for moving or renaming a file. The "move"
function has the same parameters as the "copy" function, so if you want to try using the "move" function
just substitute "move" for "copy" in the above example.
In the next example, we will check to see if a file exists and if it does then we will delete the file.
We could use java.nio.file.Files to check if the file exists and to delete the file, but instead we
will use java.io.File because it is easier to code. This example will delete the file we just copied
using the FileCopy class example.
Here is the example:
In the "main" function, we use the system properties storing the current directory and the file separator for
our operating system. After constructing the File object we call the function "exists" to see if the file
exists. If it doesn't exist then we print a line to standard output and exit the program. If it does exist
then we call the "delete" function on the File object, and finish the program by printing a line stating
that the file was deleted.
In this lesson, you learned how to use javax.swing.JFileChooser to select java.io.File objects. You also
learned how to use java.nio.file.Files to "copy" a file. Lastly, you learned how to use java.io.File
to check if a file exists and how to delete a file.