In this program we are going to make a temporary file, which will be deleted automatically by the garbage collector when the program ends.
In this program we are using following methods:
createTempFile(String prefix, String suffix): This is a static method of file class. This creates an empty file and we have used two parameters to generate name.
write(): It will write in String.
close(): When we don't need to write further, we should close the program with() that will close the string.
deleteOnExit(): It will delete the existing file when you close the java program or your JVM.
Code of this program is given below:
The output of this program is given below:
|C:\ CreateTemporaryFile>java CreateTemporaryFile
Temporary file file has been created : C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\prefix3
Liked it! Share this Tutorial