|
| 1 | +package com.baeldung.tempfile |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import java.nio.file.Path |
| 5 | +import java.nio.file.Paths |
| 6 | +import kotlin.io.path.createTempFile |
| 7 | +import kotlin.io.path.writeText |
| 8 | + |
| 9 | +fun createTempFileWithKotlinExtensions(): Path { |
| 10 | + val tempFile = createTempFile(prefix = "kotlinTemp", suffix = ".tmp") |
| 11 | + println("Temporary file created with Kotlin extensions at: ${tempFile.toAbsolutePath()}") |
| 12 | + |
| 13 | + tempFile.writeText("Kotlin Path Data") |
| 14 | + |
| 15 | + tempFile.toFile().deleteOnExit() |
| 16 | + |
| 17 | + return tempFile |
| 18 | +} |
| 19 | + |
| 20 | +fun createCustomTempFileWithKotlinExtensions(): Path { |
| 21 | + val customDir = Paths.get(System.getProperty("java.io.tmpdir")) |
| 22 | + val tempFile = createTempFile(directory = customDir, prefix = "customKotlinTemp", suffix = ".tmp") |
| 23 | + println("Custom temporary file created with Kotlin extensions at: ${tempFile.toAbsolutePath()}") |
| 24 | + |
| 25 | + tempFile.writeText("Custom Kotlin Path Data") |
| 26 | + |
| 27 | + return tempFile |
| 28 | +} |
| 29 | + |
| 30 | +@Deprecated("This method is deprecated due to permissioning issues. Use kotlin.io.path.createTempFile instead.") |
| 31 | +fun deprecatedCreateTempFile(): File { |
| 32 | + val tempFile = kotlin.io.createTempFile() |
| 33 | + println("Deprecated temporary file created at: ${tempFile.absolutePath}") |
| 34 | + tempFile.writeText("Deprecated Data") |
| 35 | + return tempFile |
| 36 | +} |
| 37 | + |
| 38 | +fun createTempFile(): File { |
| 39 | + val tempFile = File.createTempFile("temp", ".tmp") |
| 40 | + println("Temporary file created at: ${tempFile.absolutePath}") |
| 41 | + tempFile.writeText("Sample Data") |
| 42 | + tempFile.deleteOnExit() |
| 43 | + return tempFile |
| 44 | +} |
| 45 | + |
| 46 | +fun createCustomTempFile(): File { |
| 47 | + val tempDir = System.getProperty("java.io.tmpdir") |
| 48 | + val tempFile = File.createTempFile("customTemp", ".tmp", File(tempDir)) |
| 49 | + tempFile.deleteOnExit() |
| 50 | + println("Custom temporary file created at: ${tempFile.absolutePath}") |
| 51 | + tempFile.writeText("Custom Data") |
| 52 | + return tempFile |
| 53 | +} |
| 54 | + |
0 commit comments