Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to open a file for writing in internal storage.
Android Kotlin
val fileOutput = openFileOutput("myfile.txt", [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using append mode when you want to overwrite
Using deprecated or insecure modes
✗ Incorrect
Use Context.MODE_PRIVATE to create or open a file that is private to your app.
2fill in blank
mediumComplete the code to read text from a file in internal storage.
Android Kotlin
val inputStream = openFileInput("myfile.txt") val text = inputStream.bufferedReader().use { it.[1]() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using readLines() which returns a list of strings
Using readBytes() which returns bytes, not string
✗ Incorrect
readText() reads the entire content of the file as a single string.
3fill in blank
hardFix the error in the code to write a string to a file safely.
Android Kotlin
val fileOutput = openFileOutput("data.txt", Context.MODE_PRIVATE) fileOutput.[1]("Hello World".toByteArray()) fileOutput.close()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using flush() instead of write()
Trying to read from output stream
✗ Incorrect
Use write() to write bytes to the file output stream.
4fill in blank
hardFill both blanks to check if a file exists and delete it.
Android Kotlin
val file = File(filesDir, "temp.txt") if (file.[1]()) { file.[2]() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using createNewFile() instead of delete()
Checking isFile() which only checks type, not existence
✗ Incorrect
Use exists() to check if the file is there, and delete() to remove it.
5fill in blank
hardFill all three blanks to write and then read a string from a file.
Android Kotlin
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]() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using append mode when overwriting is intended
Not closing streams properly
Using readLines() instead of readText()
✗ Incorrect
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.