This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes using File.createTempFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "using File.createTempFile" Watch "using File.createTempFile" New topic
Author

using File.createTempFile

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
I am trying to perform following operation, creating temp_copy. I then create FileOutputStream wrapper around it and write the content of FileInputStream fis_temp into it, using while loop.

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.



SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4163
    
  21

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...)

Steve
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: using File.createTempFile
 
Similar Threads
I want to change colour of image.
Merge and split file
Display PDF in IE from byte array using Servlet
copying file into database
how to copy a file from one folder to another folder?