Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using File.createTempFile

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic