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 Stringof 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 = openFileOutput(fileName,
Context.MODE_PRIVATE)
stream.write(content.toByteArray())
stream.close()
}

MODE_PRIVATE is an operating mode that creates a file with a given name (or replaces a file with a corresponding name) and makes it private to your application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.218.226