

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 a temporary file in Java
A temporary file 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", null); System.out.println(file.getAbsolutePath()); file.deleteOnExit(); } }
The output of the above program is as follows −
Output
C:\Users\amit_\AppData\Local\Temp\temp6072597842246154962.tmp
Now let us understand the above program.
A temporary file 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", null); System.out.println(file.getAbsolutePath()); file.deleteOnExit();
- Related Questions & Answers
- Create temporary file in specified directory in Java
- Create temporary file with specified extension suffix in Java
- How to create a temporary file using PowerShell?
- How to delete a temporary file in Java?
- How to create a unique temporary file name using Python?
- Create a temporary table in a MySQL procedure?
- Create a temporary table with dates in MySQL
- Create a new empty file in Java
- Temporary files in Java
- C# Program to display temporary file names
- How to create a pdf file in Java?
- How to generate secure temporary file name using Python?
- How to create a temporary MySQL table in a SELECT statement without a separate CREATE TABLE?
- How to create a Feature file for Cucumber in Java?
- How to Write/create a JSON file using Java?
Advertisements