HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java – Delete Temporary File

By Lokesh Gupta | Filed Under: Java I/O

If your application needs to create temporary files for some application logic or unit testing, then you would like to make sure that these temporary files are deleted when they are not needed. Let’s learn how to delete temporary files in java.

Deleting temporary file using File.deleteOnExit() or File.delete()

To delete a file when apllication exists or completes, you can use :

File temp = File.createTempFile("myTempFile", ".txt"); 
temp.deleteOnExit();

To delete the file, immediately without waiting for application exit, you can directly use delete() method.

File temp = File.createTempFile("myTempFile", ".txt"); 
temp.delete();

Example Code for deleting temporary file

import java.io.File;
import java.io.IOException;

public class TemporaryFileExample
{
   public static void main(String[] args)
   {
      File temp;
      try
      {
         temp = File.createTempFile("myTempFile", ".txt");
         
         System.out.println("Temp file created : " + temp.getAbsolutePath());
         
         //temp.delete(); //For deleting immediately
         
         temp.deleteOnExit(); //Delete on runtime exit
         
         System.out.println("Temp file exists : " + temp.exists());
      } catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}

Writing data to temporary file using NIO

If you want to use java NIO library, then you can use Files.delete() or Files.deleteIfExists() methods.

public class TemporaryFileExample
{
   public static void main(String[] args)
   {
      try
      {
         final Path path = Files.createTempFile("myTempFile", ".txt");
         System.out.println("Temp file : " + path);

         //Delete file on exit
         Files.deleteIfExists(path);
		 
		 //Delete file immediately
		 Files.delete(path);	
         
      } catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}

Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

3
Leave a Reply

This comment form is under antispam protection
2 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
John

Files.deleteIfExists() is not equivalent to File.deleteOnExit() does. It simply deletes the file immediately.

Vote Up0Vote Down  Reply
3 months ago
joseph

If file is under /tmp, it would automatically be deleted by the OS right?

Vote Up0Vote Down  Reply
5 years ago
Lokesh Gupta

Not necessarily. As much I know, nothing happens automatically.. 🙂

Vote Up0Vote Down  Reply
5 years ago

Search Tutorials

Java IO Tutorials

  • IO – Introduction
  • IO – How it works?
  • IO – IO vs. NIO
  • IO – Copy Directory Recursively
  • IO – Delete Directory Recursively
  • IO – Create File
  • IO – Write to File
  • IO – Append to File
  • IO – Create Read Only File
  • IO – Read File to String
  • IO – Read File to Byte[]
  • IO – Read File Line by Line
  • IO – BufferedReader
  • IO – BufferedWriter
  • IO – Read/Write Properties File
  • IO – Read file from resources
  • IO – Read/Write UTF-8 Data
  • IO – Check if File Exist
  • IO – File Copy
  • IO – FilenameFilter
  • IO – FileFilter
  • IO – Create Temporary File
  • IO – Write Temporary File
  • IO – Delete Temporary File
  • IO – Read from Console
  • IO – Typesafe input using Scanner
  • IO – String to InputStream
  • IO – InputStream to String
  • IO – Password Protected Zip
  • IO – Unzip with Sub-directories
  • IO – Manage System Log Size
  • IO – Generate SHA / MD5

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz