0
0
Android Kotlinmobile~10 mins

File access and storage in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AContext.MODE_WORLD_READABLE
BContext.MODE_APPEND
CContext.MODE_MULTI_PROCESS
DContext.MODE_PRIVATE
Attempts:
3 left
💡 Hint
Common Mistakes
Using append mode when you want to overwrite
Using deprecated or insecure modes
2fill in blank
medium

Complete 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'
AreadLines
BreadText
CreadBytes
DreadChars
Attempts:
3 left
💡 Hint
Common Mistakes
Using readLines() which returns a list of strings
Using readBytes() which returns bytes, not string
3fill in blank
hard

Fix 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'
Aclose
Bflush
Cwrite
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Using flush() instead of write()
Trying to read from output stream
4fill in blank
hard

Fill 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'
Aexists
Bdelete
CcreateNewFile
DisFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using createNewFile() instead of delete()
Checking isFile() which only checks type, not existence
5fill in blank
hard

Fill 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'
AContext.MODE_PRIVATE
BreadText
Cuse
DContext.MODE_APPEND
Attempts:
3 left
💡 Hint
Common Mistakes
Using append mode when overwriting is intended
Not closing streams properly
Using readLines() instead of readText()