android-studio
  1. android-studio-internal-storage

Android Studio Internal Storage

Syntax

val file = File(context.filesDir, filename)
file.writeText(text)
val text = file.readText()

Example

val filename = "example.txt"
val text = "This is an example text!"
val file = File(context.filesDir, filename)
file.writeText(text)
val readText = file.readText()
Log.d(TAG, "Text written: $text")
Log.d(TAG, "Text read: $readText")

Output

D/MainActivity: Text written: This is an example text!
D/MainActivity: Text read: This is an example text!

Explanation

Internal storage is a private storage space which is only accessible to the app that created it. In Android Studio, we can use the context.filesDir method to get the path of internal storage. Then, we can create a File object with this path and a filename to read from or write to the internal storage.

In the example above, the code writes a text string to a file named "example.txt". It then reads the text from the same file and logs both the original text and the read text to the console.

Use

The internal storage is useful for saving app-specific data that should not be accessible to other apps or users. Examples of data that can be stored in the internal storage include user preferences, cache files, and other private data.

Important Points

  • The internal storage is private and only accessible to the app that created it.
  • Data stored in the internal storage is not accessible to other apps or users.
  • To write to a file in the internal storage, we can use the writeText() method of the File class.
  • To read from a file in the internal storage, we can use the readText() method of the File class.

Summary

Android Studio's internal storage is a private storage space that provides data persistence for an app. With the context.filesDir method, we can get the path of the internal storage, create a File object with this path and a filename, and use the writeText() and readText() methods of the File class to write or read text from the internal storage. The internal storage is useful for saving app-specific data that should not be accessible to other apps or users.

Published on: