Share on Google+Share on Google+

Create a temporary file

Advertisement
In this section, you will learn how to create a temporary file. This will perform with the help of createTempFile() method of File class.

Create a temporary file

In this section, you will learn how to create a temporary file. This will perform with the help of  createTempFile() method of File class.

The syntax and description of createTempFile() method is given below :


public static File createTempFile(String prefix, String suffix) throws IOException 

Description

This method creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. 

Example :

package roseindia;

import java.io.*;

public class CreateTempFile {
public static void main(String[] args) throws Exception {
File temp = File.createTempFile("roseindia", ".tmp");
temp.deleteOnExit();
BufferedWriter out = new BufferedWriter(new FileWriter(temp));
out.write("Welcome to Roseindia");
System.out.println("temporary file created");
out.close();
}

}

Output : 

temporary file created:                                               

Download Source Code

Advertisements

Advertisement

Posted on: February 17, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.