How to Delete Temporary File in Java


The example will tell user how to delete temporary file in Java.

Source Code

package com.beginner.examples;

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

public class DeleteTemporaryFile {

	public static void main(String[] args) {
		
		try {
			
			File tmpFile = File.createTempFile("tmpFile", "json");
			String tmpFilePath = tmpFile.getAbsolutePath();
			System.out.println(tmpFilePath);
			
			tmpFile.deleteOnExit();
			System.out.println("File deleted");
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		

	}

}

Output:

C:UsersADMINI~1AppDataLocalTemptmpFile4841561436302701840json
File deleted

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments