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:
App private directories
- context.getCacheDir()
- context.getExternalCacheDir()
- and ... (full list can be found here)
Public directories
- Environment.getExternalStorageDirectory()
- Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES)
- and ... (full list can be found here)
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.
This is what I typically do:
File outputDir = context.getCacheDir(); // context being the Activity pointer
File outputFile = File.createTempFile("prefix", ".extension", outputDir);
As for their deletion, I am not complete sure either. Since I use this in my implementation of a cache, I manually delete the oldest files till the cache directory size comes down to my preset value.
Best practices on internal and external temporary files:
Internal Cache
If you'd like to cache some data, rather than store it persistently, you should use
getCacheDir()to open a File that represents the internal directory where your application should save temporary cache files.When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.
External Cache
To open a File that represents the external storage directory where you should save cache files, call
getExternalCacheDir(). If the user uninstalls your application, these files will be automatically deleted.Similar to
ContextCompat.getExternalFilesDirs(), mentioned above, you can also access a cache directory on a secondary external storage (if available) by callingContextCompat.getExternalCacheDirs().Tip: To preserve file space and maintain your app's performance, it's important that you carefully manage your cache files and remove those that aren't needed anymore throughout your app's lifecycle.