🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.io.path › create-temp-file.html
createTempFile | Core API – Kotlin Programming Language
kotlin-stdlib/kotlin.io.path/createTempFile · JVM · inline fun createTempFile(prefix: String? = null, suffix: String? = null, vararg attributes: FileAttribute<*>): Path(source) Creates an empty file in the default temp directory, using the given prefix and suffix to generate its name.
🌐
Baeldung
baeldung.com › home › kotlin › kotlin io › how to create a temporary file in kotlin
How to Create a Temporary File in Kotlin | Baeldung on Kotlin
May 31, 2024 - To prove we can use the file, we call writeText() to populate the file with some content. To delete the temp file on exit, we need to transform the Path to a File and then specify that it should be deleted on exit:
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.io › create-temp-file.html
createTempFile - Kotlin Programming Language
July 9, 2021 - Creates a new empty file in the specified directory, using the given prefix and suffix to generate its name. If prefix is not specified then some unspecified string will be used. If suffix is not specified then ".tmp" will be used.
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.io › create-temp-dir.html
createTempDir | Core API – Kotlin Programming Language
createTempDir(prefix: String = "tmp", suffix: String? = null, directory: File? = null): File(source) ... Avoid creating temporary directories in the default temp location with this function due to too wide permissions on the newly created directory. Use kotlin.io.path.createTempDirectory instead.
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.io › create-temp-dir.html
createTempDir - Kotlin Programming Language
September 5, 2022 - Try the revamped Kotlin docs design! ... createTempDir( prefix: String = "tmp", suffix: String? = null, directory: File? = null ): File (source) Deprecated: Avoid creating temporary directories in the default temp location with this function due to too wide permissions on the newly created directory.
🌐
K210
k210.org › dev › android_create_temp_file_in_cache_directory_kotlin
Android Create Temp File In Cache Directory (Kotlin) - Android - K210.ORG
August 8, 2025 - val timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-kkmmss")) val outputFile = File(context.cacheDir, "output-${timestamp}.txt") Create a temp file in cache directory.
🌐
Kotest
kotest.io › temporary files
Temporary Files | Kotest
But in case the assertion fails ... the next run (file cannot be overwritten exception and so on). Kotest provides a function tempfile() which you can use in your Spec to create a temporary file for your tests, and which Kotest will take the responsibility of cleaning up after ...
🌐
Luasoftware
code.luasoftware.com › tutorials › android › android-create-cache-file
Android Create Temp File In Cache Directory (Kotlin)
val timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-kkmmss"))val outputFile = File(context.cacheDir, "output-${timestamp}.txt") Create a temp file in cache directory.
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.io.path › create-temp-directory.html
createTempDirectory | Core API – Kotlin Programming Language
kotlin-stdlib/kotlin.io.path/createTempDirectory · JVM · inline fun createTempDirectory(prefix: String? = null, vararg attributes: FileAttribute<*>): Path(source) Creates a new directory in the default temp directory, using the given prefix to generate its name.
🌐
W3cubDocs
docs.w3cub.com › kotlin › api › latest › jvm › stdlib › kotlin.io › create-temp-file
kotlin.io.createTempFile - Kotlin - W3cubDocs
Creates a new empty file in the specified directory, using the given prefix and suffix to generate its name. If prefix is not specified then some unspecified string will be used. If suffix is not specified then ".tmp" will be used. If directory is not specified then the default temporary-file ...
Find elsewhere
🌐
GitHub
github.com › Kotlin › kotlinx-io › issues › 391
Temporary file creation support · Issue #391 · Kotlin/kotlinx-io
September 13, 2024 - Hello, it's possible to create a temporary file like in jvm File.createTempFile()
Author   Kotlin
Top answer
1 of 5
48

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)
2 of 5
34

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.

🌐
LearnXbyExample
learnxbyexample.com › learn x by example › kotlin programming › temporary files and directories in kotlin
Temporary Files And Directories in Kotlin
September 22, 2024 - Temporary files and directories ... Exception?) { e?.let { throw it } } fun main() { // The easiest way to create a temporary file is by using // createTempFile....
🌐
Shinyu
shinyu.org › en › kotlin › files-and-io › creating-a-temporary-file
Kotlin: Creating a temporary file
March 14, 2024 - import java.io.File fun main() { val tempFile = File.createTempFile("myTempFile", ".tmp") println("Temporary file created at: ${tempFile.absolutePath}") // Write to temp file tempFile.writeText("Kotlin is pretty neat, huh?") // Delete on exit tempFile.deleteOnExit() }
🌐
Typeerror
typeerror.org › docs › kotlin~1.6 › api › latest › jvm › stdlib › kotlin.io.path › create-temp-file
kotlin.io.path.createTempFile - Kotlin 1.6 Documentation - TypeError
createTempFile kotlin-stdlib / kotlin.io.path / createTempFile Platform and version requirements: JVM (1.5), JRE7 (1.5) fun createTempFile( prefix: String? = null, suffix: String? = null, vararg attributes: FileAttribute ): Path Creates an empty file in the default temp directory, using the given prefix and suffix to generate its name.
Top answer
1 of 6
348

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.

2 of 6
97

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 calling ContextCompat.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.

🌐
Kotlin
kotlinlang.org › api › kotlinx-io › kotlinx-io-core › kotlinx.io.files › -system-temporary-directory.html
SystemTemporaryDirectory | kotlinx-io – Kotlin Programming Language
kotlinx-io-core/kotlinx.io.files/SystemTemporaryDirectory · applecommonjvmnativeNonApplenodeFilesystemSharedwasmWasi · actual val SystemTemporaryDirectory: Path(source) Path to a directory suitable for creating temporary files.
🌐
JetBrains
youtrack.jetbrains.com › issue › KT-43306 › Deprecate-createTempFile-and-createTempDir-functions-in-kotlinio
Deprecate createTempFile and createTempDir functions in ...
November 11, 2020 - {{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
GitHub
github.com › illarionov › tempfolder-kmp
GitHub - illarionov/tempfolder-kmp: Temporary directories for Kotlin Multiplatform · GitHub
A library for creating temporary directories in Kotlin Multiplatform projects.
Author   illarionov