Java Reference
In-Depth Information
Youprobablywanttoremovethetemporaryfileaftertheusertellstheapplicationto
save or discard the changes. The
deleteOnExit()
method lets you register a tem-
porary file for deletion; it's deleted when the JVM ends without a crash/power loss.
createTempFile()
and
deleteOnExit()
methods.
Listing 8-5.
Experimenting with temporary files
import java.io.File;
import java.io.IOException;
class TempFileDemo
{
public static void main(String[] args) throws IOExcep-
tion
{
Sys-
tem.out.println(System.getProperty("java.io.tmpdir"));
File temp = File.createTempFile("text", ".txt");
System.out.println(temp);
temp.deleteOnExit();
}
}
Afteroutputtingthelocationwheretemporaryfilesarestored,
TempFileDemo
cre-
ates a temporary file whose name begins with
text
and has extension
.txt
.
Tem-
pFileDemo
nextoutputsthetemporaryfile'snameandregistersthetemporaryfilefor
deletion upon the successful termination of the application.
Iobservedthefollowingoutputduringonerunof
TempFileDemo
(andthefiledis-
appeared on exit):
C:\DOCUME~1\JEFFFR~1\LOCALS~1\Temp\
C:\DOCUME~1\JEFFFR~1\LOCALS~1\Temp\text3436502412322813057.
txt
Note
Java6addedto
File
new
boolean setExecutable(boolean ex-
ecutable)
,
boolean setExecutable(boolean executable,
boolean ownerOnly)
,
boolean setReadable(boolean readable)
,
boolean setReadable(boolean readable, boolean ownerOnly)
,