Sobes.tech
Junior — Middle

How can data be saved in an Android application without using Room libraries and SharedPreferences?

sobes.tech AI

Answer from AI

Data can be stored in Android without using Room and SharedPreferences by using the following methods:

  • Files in internal or external storage:

    • Internal storage (getFilesDir()) for private application files.
    • External storage (getExternalFilesDir()) for files accessible to the user.
  • Direct SQLite:

    • Using the SQLiteOpenHelper class to create and manage a database without Room.
  • Serialization to a file:

    • Saving objects to a file in JSON, XML, or other formats, with subsequent deserialization.

Example of writing a string to an internal file:

String filename = "myfile.txt";
String fileContents = "Hello World";
try (FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE)) {
    fos.write(fileContents.getBytes());
} catch (IOException e) {
    e.printStackTrace();
}

Thus, data can be stored without using Room and SharedPreferences by choosing the appropriate method depending on the task.