Writing files to internal storage

In order to create a private file on internal storage, it is necessary to call openFileOutput(). The openFileOutput() function takes two arguments. The first is the name (in form of a String) of the file to open and the second is the operating mode. Note that openFileOutput() must be called within an instance of Context, such as an Activity.

The openFileOutput() method returns a FileOutputStream. This can then be used to tell the file it is done with the write() method. Once writing is done, the FileOutputStream should be closed by calling its close() method. The following code snippet demonstrates this process:

private fun writeFile(fileName: String) {  val content: String = "Hello world"  val stream: FileOutputStream ...

Get Kotlin Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.