This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Naresh Chaurasia wrote:Is there any performance benefit of using File.createTempFile, since the file is getting created in temp directory in my windows machine(compared to using new File()).
The problem with using File.createTempFile() is that a random number is appended to the name of the file.
No, there is no performance gain. The reason you use createTempFile() instead of naming the file yourself:
1) createTempFile() makes it clear the purpose of the file is to last a short time
2) createTempFile() puts the file in the OS-specific default temp location. This saves you from having to figure out where that is, or from putting temp files in a non-temp storage area
3) Using the temp file location helps the OS keep temporary storage in check.
4) createTempFile() ensures that the file being created is unique. Thus the random number in the name. Without it you have to either write your own routine to ensure uniqueness or be okay with overwriting previous data if that file already exists (which you may not have permissions to do...)