Complete the code to open a file for writing in internal storage.
val fileOutput = openFileOutput("myfile.txt", [1])
Use Context.MODE_PRIVATE to create or open a file that is private to your app.
Complete the code to read text from a file in internal storage.
val inputStream = openFileInput("myfile.txt") val text = inputStream.bufferedReader().use { it.[1]() }
readText() reads the entire content of the file as a single string.
Fix the error in the code to write a string to a file safely.
val fileOutput = openFileOutput("data.txt", Context.MODE_PRIVATE) fileOutput.[1]("Hello World".toByteArray()) fileOutput.close()
Use write() to write bytes to the file output stream.
Fill both blanks to check if a file exists and delete it.
val file = File(filesDir, "temp.txt") if (file.[1]()) { file.[2]() }
Use exists() to check if the file is there, and delete() to remove it.
Fill all three blanks to write and then read a string from a file.
val filename = "log.txt" val output = openFileOutput(filename, [1]) output.write("Test Entry".toByteArray()) output.close() val input = openFileInput(filename) val content = input.bufferedReader().[2] { it.[3]() }
Use Context.MODE_PRIVATE to open the file for writing, then use to safely handle the reader, and readText() to get the file content as a string.
