Creating and Manipulating Files and Directories
files and directories. Table 11-4 describes these methods.
Table 11-4. File Methods for Creating New and Manipulating Existing Files and Directories
Method Description
boolean createNewFile() Atomically creates a new, empty file named by this File object’s abstract
pathname if and only if a file with this name doesn’t yet exist. The check
for file existence (and the creation of the file when it doesn’t exist) is a
single operation that’s atomic with respect to all other filesystem activities
that might affect the file. This method returns true when the named file
doesn’t exist and was successfully created, and returns false when the
named file already exists. It throws IOException when an I/O error occurs.
static File createTempFile(String prefix,
String suffix)
Creates an empty file in the default temporary file directory using the
given prefix and suffix to generate its name. This overloaded class
method calls its three-parameter variant, passing prefix, suffix, and
null to this other method, and returning the other method’s return value.
这个重载类方法调用它的三参数变体。
Suppose you’re designing a text-editor application that a user will use to open a text file and make
changes to its content. Until the user explicitly saves these changes to the file, you want the text file
to remain unchanged.
Because the user doesn’t want to lose these changes when the application crashes or the computer
loses power, you design the application to save these changes to a temporary file every few minutes.
This way, the user has a backup of the changes.
You can use the overloaded createTempFile() methods to create the temporary file. If you don’t
specify a directory in which to store this file, it’s created in the directory identified by the
java.io.tmpdir system property.
You probably want to remove the temporary file after the user tells the application to save or discard
the changes. The deleteOnExit() method lets you register a temporary file for deletion; it’s deleted
when the virtual machine ends without a crash/power loss.
Listing 11-6 presents a TempFileDemo application for experimenting with the createTempFile() and
deleteOnExit() methods.
Listing 11-6. Experimenting with Temporary Files
import java.io.File;
import java.io.IOException;
public class TempFileDemo
{
public static void main(String[] args) throws IOException
{
System.out.println(System.getProperty("java.io.tmpdir"));
File temp = File.createTempFile("text", ".txt");
System.out.println(temp);
temp.deleteOnExit();
}
}
After outputting the location where temporary files are stored, TempFileDemo creates a temporary
file whose name begins with text and ends with the .txt extension. TempFileDemo next outputs the
temporary file’s name and registers the temporary file for deletion upon the successful termination of
the application.
I observed the following output during one run of TempFileDemo (and the file disappeared on exit):
C:\Users\Owner\AppData\Local\Temp\
C:\Users\Owner\AppData\Local\Temp\text3173127870811188221.txt