Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.createTempFile() VS new File()

In my app I added the possibility to add and save pictures for everyday day, represented by a Fragment.

In order to save the picture on the SD card, I currently use the function File.createTempFile:

File imageFile = File.createTempFile(imageFileName, MyApplication.JPEG_FILE_SUFFIX, MyApplication.getAlbumDir()); 

I actually also tried with the standard way:

File imageFile = new File(MyApplication.getAlbumDir(), imageFileName + MyApplication.JPEG_FILE_SUFFIX); 

Both are working, but in my case the first one is better than the second one because the function createTempFile adds a long random number at the end of the file name making it unique.

To give you an example, here is what I get with both methods:

With createTempFile: IMG_2013-06-18_-1961144729.jpg

With new File: IMG_2013-06-18_.jpg

Finally my question is, is it safe to keep using createTempFile to save my pictures or do I have to use the standard way and add some code to generate a unique file name? Should it only be used for temporary files?

I reviewed the documentation about the function but I did not find anything about the possible consequences of using it instead of new File.

Thank you

like image 962
Yoann Hercouet Avatar asked Jun 17 '13 14:06

Yoann Hercouet


People also ask

How do you create a temp file in Java without the random number appended to the file name?

Just check the return value of temp. createNewFile() . Read the specification of createNewFile() . The important word is atomic.

How do I create a temp file?

To create and use a temporary fileThe application opens the user-provided source text file by using CreateFile. The application retrieves a temporary file path and file name by using the GetTempPath and GetTempFileName functions, and then uses CreateFile to create the temporary file.


2 Answers

1. Create file with random name

File file = File.createTempFile(String prefix, String suffix, File parent) 
  • Actually create the file on disk and returns the file object
  • Create a file name in this format: prefix + random number + suffix
  • Useful when you need create temporary file on disk

2. Create file with exact name

File file = new File(File parent, String child); file.createNewFile(); 
  • Actually create the file on disk and returns true if file get created successfully

  • File name will exactly as pass to child parameter

  • Useful when you need create permanent file on disk

3. Create only file object (in memory)

File file = new File(File parent, String child); // doesn't create the file on disk until calling createNewFile() method 
  • Only create the in memory and not actually on disk
  • Useful when you need just create file object (e.g just to pass it as parameter)

parent parameter can be one of these:

  1. App private directories

    • context.getCacheDir()
    • context.getExternalCacheDir()
    • and ... (full list can be found here)
  2. Public directories

    • Environment.getExternalStorageDirectory()
    • Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES)
    • and ... (full list can be found here)
like image 61
Behzad Bahmanyar Avatar answered Oct 11 '22 09:10

Behzad Bahmanyar


Sounds like your app is creating files, so you need to guarantee unique filenames. You could keep some kind of counter within your app (saved to preferences or DB) and use that. Then you could create shorter/more controlled names, and control uniqueness yourself. Or you can use createTempFile(), which will guarantee you get a unique filename (but you only get partial control of the filename). Sounds like you prefer createTempFile(), so there's no reason not to continue using it if you are happy with the filenames it generates. There's no down side other than not having full control over the filename format.

like image 31
user1676075 Avatar answered Oct 11 '22 09:10

user1676075