'How to save a file on Mac using Compose Multiplatform for Desktop
I'm trying to save a text file with the app logs in my compose multiplatform project on a Mac, but it only works with debug builds. The distributable version throws an exception at runtime.
If I create the distributable via gradle packageDmg and I open the generated app I get the following exception:
java.nio.file.FileSystemException: /logs: Read-only file system
The app tries to create a folder relative to it's path, named logs where it should put a txt file.
If I run it locally via IntelliJ IDEA it creates the file in the specified folder as expected.
This is the code I'm running:
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.*
object FileUtils {
fun getOrCreateFile(dirName: String, fileName: String): File {
Paths.get(dirName).createDirectories()
val file = Paths.get(dirName, fileName)
if (!Files.exists(file)) {
file.createFile()
}
return file.toFile()
}
}
fun saveLogsToFile(text: String) {
val today = SimpleDateFormat("dd-M-yyyy").format(Date())
val file = FileUtils.getOrCreateFile(
"logs",
"$today.txt"
)
FileOutputStream(file, true).bufferedWriter().use { fileWriter ->
fileWriter.write(text + "\n")
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
