- 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
Create temporary file with specified extension suffix in Java
A temporary file with the specified extension suffix can be created using the method java.io.File.createTempFile(). This method requires two parameters i.e. the prefix to define the file name and the suffix to define the file extension. It also returns the abstract path name for the temporary file created.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) throws Exception { File file = File.createTempFile("temp", ".java"); System.out.println(file.getAbsolutePath()); file.deleteOnExit(); } }
The output of the above program is as follows −
Output
C:\Users\amit_\AppData\Local\Temp\temp3879834079458072093.java
Now let us understand the above program.
A temporary file with the specified extension suffix is created using the method java.io.File.createTempFile(). Then the file path is displayed. Also, the method java.io.File.deleteOnExit() is used to delete the temporary file after the program ends. A code snippet that demonstrates this is given as follows −
File file = File.createTempFile("temp", ".java"); System.out.println(file.getAbsolutePath()); file.deleteOnExit();
- Related Articles
- Create temporary file in specified directory in Java
- Create a temporary file in Java
- Get file extension name in Java
- How to create a temporary file using PowerShell?
- How to delete a temporary file in Java?
- Perl File Extension
- How to create a unique temporary file name using Python?
- Create a temporary table with dates in MySQL
- How to determine if an input string ends with a specified suffix in JSP?
- How to check if a string ends with a specified Suffix string in Golang?
- Get only the file extension from a column with file names as strings in MySQL?
- How to change file extension in Python?
- How to write Python regular expression to match with file extension?
- How to extract file extension using Python?
- Temporary files in Java
