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)
android - File.createTempFile() vs. new File() - Stack Overflow
What is a safe way to create a Temp file in Java? - Stack Overflow
Using createTempFile() method?
Need some clarity on usage of createTempFile.
Videos
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.
Use File.createTempFile().
File tempFile = File.createTempFile("prefix-", "-suffix");
//File tempFile = File.createTempFile("MyAppName-", ".tmp");
tempFile.deleteOnExit();
Will create a file in the temp dir, like:
prefix-6340763779352094442-suffix
NIO.2
Since Java 7 there is the new file API "NIO2" which contains new methods for creating temp files and directories.
| Modern | Use default temp folder | Specify folder |
|---|---|---|
| Create folder | Files.createTempDirectory |
Files.createTempDirectory |
| Create file | Files.createTempFile |
Files.createTempFile |
e.g.
Path tempDir = Files.createTempDirectory("tempfiles");
or
Path tempFile = Files.createTempFile("file", ".tmp");
For more info:
- Oracle’s online tutorial, free-of-cost: File I/O (Featuring NIO.2)
- Oracle technical notes: NIO and NIO.2 Examples
- What Is the Difference Between NIO and NIO.2? at Baeldung.com.
- Introduction to the Java NIO2 File API at Baeldung.com.
Legacy
Before Java 7, you might use the legacy methods createTempFile found on the File class. Notice the name “File” is singular, in contrast to the Files class in modern NIO.2.
| Legacy | Use default temp folder | Specify folder |
|---|---|---|
| Create folder | — | — |
| Create file | File.createTempFile |
File.createTempFile |
Security note
An important difference between File.createTempFile() (singular File) and Files.createTempFile (plural Files) is also that the latter has more secure permission defaults.
When no file attributes are specified, then the resulting file may have more restrictive access permissions to files created by the
File.createTempFile(String,String,File)method.