Publish Your Article Here

Program: How to delete temporary file in java?

Below example shows how to delete a temporary file in java. The method deleteOnExit(), deletes the temp file on program exists. The method delete() deletes the file immediately.

package com.java2novice.files;


public class MyTempDelete {

	public static void main(String a[]){
		File tempFile = null;
		BufferedWriter writer = null;
		try {
			tempFile = File.createTempFile("MyTempFile", ".tmp");
			writer = new BufferedWriter(new FileWriter(tempFile));
			writer.write("Writing data into temp file!!!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
		} finally {
				if(writer != null) writer.close();
			}catch(Exception ex){}
		System.out.println("Stored data in temporary file.");
		 * Write your application logic here
		 * Once your application done with the temp data,
		 * delete the temp file
		//below method deletes the temp file on program exists.
		//below method deletes the temp file immediately

Stored data in temporary file.
<< Previous Program | Next Program >>
blog comments powered by Disqus

Java File IO Operations Sample Code Examples

  1. All file operations.
  2. Show list of all file names from a folder.
  3. How to get list of all files from a folder?
  4. Filter the files by file extensions and show the file names.
  5. How to read file content using byte array?
  6. How to read file content line by line in java?
  7. How to read property file in static context?
  8. How to read input from console in java?
  9. How to get file list from a folder filtered by extensions?
  10. How to get file URI reference?
  11. How to store and read objects from a file?
  12. How to create and store property file dynamically?
  13. How to store property file as xml file?
  14. How to get file last modified time?
  15. How to convert byte array to inputstream?
  16. How to convert inputstream to reader or BufferedReader?
  17. How to convert byte array to reader or BufferedReader?
  18. How to set file permissions in java?
  19. How to read a file using BufferedInputStream?
  20. How to create temporary file in java?
  21. How to write or store data into temporary file in java?
  22. How to delete temporary file in java?
  23. How to write string content to a file in java?
  24. How to write byte content to a file in java?
Knowledge Centre
doPost Vs doGet methods
doGet() method is used to get information, while doPost() method is used for posting information. doGet() requests can't send large amount of information and is limited to 240-255 characters. However, doPost()requests passes all of its data, of unlimited length. A doGet() request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.