Challenge - 5 Problems
File Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Reading a file from internal storage
What will be the output shown in the TextView after running this code snippet that reads a file named "data.txt" from internal storage?
Android Kotlin
val fileInput = openFileInput("data.txt")
val content = fileInput.bufferedReader().use { it.readText() }
textView.text = contentAttempts:
2 left
💡 Hint
openFileInput reads the file content as a stream, which you convert to text.
✗ Incorrect
openFileInput opens the file for reading. Using bufferedReader and readText() reads the entire file content as a string, which is then set to the TextView.
❓ lifecycle
intermediate2:00remaining
File writing in Activity lifecycle
If you write data to a file in the onPause() method of an Activity, what happens if the app is killed before onPause() is called?
Attempts:
2 left
💡 Hint
onPause() is called before the Activity goes to background, but not if the app is killed abruptly.
✗ Incorrect
If the app is killed abruptly (e.g., by the system or user), onPause() may not be called, so any file writing code there will not run and data won't be saved.
📝 Syntax
advanced2:00remaining
Correct syntax for writing text to a file
Which Kotlin code snippet correctly writes the string "Hello World" to a file named "output.txt" in internal storage?
Attempts:
2 left
💡 Hint
openFileOutput returns a FileOutputStream which you can write bytes to.
✗ Incorrect
Option C correctly uses openFileOutput with mode and writes bytes using use block. Option C is invalid because FileOutputStream has no writeText method. Option C tries to write to a file without path and wrong method. Option C misses mode parameter and write method is invalid.
🔧 Debug
advanced2:00remaining
Diagnosing a FileNotFoundException
An app crashes with FileNotFoundException when trying to read "config.txt" using openFileInput("config.txt"). What is the most likely cause?
Attempts:
2 left
💡 Hint
openFileInput only reads files previously saved in internal storage by the app.
✗ Incorrect
FileNotFoundException occurs if the file does not exist in internal storage. openFileInput does not require absolute paths and does not access external storage, so permissions or path are not the issue here.
🧠 Conceptual
expert2:00remaining
Choosing storage for sensitive data
Which storage option is best for saving sensitive user data like passwords in an Android app?
Attempts:
2 left
💡 Hint
Sensitive data should be stored where only your app can access it.
✗ Incorrect
Internal storage files with MODE_PRIVATE are only accessible by your app, making them suitable for sensitive data. External storage and cache directories can be accessed by other apps. SharedPreferences without encryption is not secure for sensitive data.