- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete a temporary file in Java?
The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.
Temporary files
In certain scenarios such as unit testing, or for some application logics you might need to create temporary files.
The File class in Java provides a method with name createTempFile(). This method accepts two String variables representing the prefix (starting name) and suffix(extension) of the temp file and a File object representing the directory (abstract path) at which you need to create the file.
Example
Following Java example creates a temporary file named exampleTempFile5387153267019244721.txt in the path D:/SampleDirectory
import java.io.File; import java.io.IOException; public class TempararyFiles { public static void main(String args[]) throws IOException { String prefix = "exampleTempFile"; String suffix = ".txt"; //Creating a File object for directory File directoryPath = new File("D:/SampleDirectory"); //Creating a temp file File.createTempFile(prefix, suffix, directoryPath); System.out.println("Temp file created........."); } }
Output
Temp file created.........
Deleting the temporary file
You can delete a temporary file in two ways using the File class and, using the Files class.
Using File class
The File class provides a delete() method which deletes the current file or directory, invoke this method on the temporary file.
Example
The following Java program creates and deletes a temp file.
import java.io.File; import java.io.IOException; public class TempararyFiles { public static void main(String args[]) throws IOException { String prefix = "exampleTempFile"; String suffix = ".txt"; //Creating a File object for directory File directoryPath = new File("D:/SampleDirectory"); //Creating a temp file File tempFile = File.createTempFile(prefix, suffix, directoryPath); System.out.println("Temp file created: "+tempFile.getAbsolutePath()); //Deleting the file tempFile.delete(); System.out.println("Temp file deleted........."); } }
Output
Temp file created: D:\SampleDirectory\exampleTempFile7179732984227266899.txt Temp file deleted.........
Using the Files class
Just like the file class the Files class of java.nio package provides createTempFile() method which accepts two String parameters representing the prefix and suffix of and creates a temp file with specified details.
The delete() method of this class accepts a path object and deletes the file in the specified path.
Example
Following Java program creates and deletes a temporary file using the using the Files class.
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; public class TempararyFiles { public static void main(String args[]) throws IOException { String prefix = "exampleTempFile"; String suffix = ".txt"; //Creating a File object for directory File directoryPath = new File("D:/SampleDirectory"); //Creating a temp file Path tempFilePath = Files.createTempFile(prefix, suffix); System.out.println("Temp file created: "+tempFilePath.toString()); //Deleting the file Files.deleteIfExists(tempFilePath); System.out.println("Temp file deleted........."); } }
Output
Temp file created: C:\Users\TUTORI~2\AppData\Local\Temp\exampleTempFile1192122004600989866.txt Temp file deleted.........
- Related Articles
- Create a temporary file in Java
- How to delete a string inside a file(.txt) in java?
- How to create a temporary file using PowerShell?
- Create temporary file in specified directory in Java
- Creating a Temporary File in Linux
- How can I delete MySQL temporary table?
- Create temporary file with specified extension suffix in Java
- How to create a unique temporary file name using Python?
- Java program to delete certain text from a file
- How to delete a file using Python?
- Java Program to delete file or directory
- Java program to delete duplicate lines in text file
- Delete the file or directory in Java
- How to generate secure temporary file name using Python?
- Delete a file in C#
