SoFunction
Updated on 2025-04-05

What are the Android data storage methods?

The following content introduces five ways to Android data storage:

1、SharedPreferences

2. File storage

3. SQLite database

4、ContentProvider

5. Network storage

This article mainly introduces how to use files to store data. Android file operations use the FileOutputStream and FileInputStream classes.

1. Store files

First instantiate a FileOutputStream.

FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
// fileName: The name of the file to be written
// MODE_PRIVATE: It is the default operation mode, which means that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file.
// MODE_APPEND: The mode will check whether the file exists. If it exists, add content to the file, otherwise a new file will be created.
// MODE_WORLD_READABLE: It means that the current file can be read by other applications, and it is not recommended to use it.
// MODE_WORLD_WRITEABLE: It means that the current file can be written by other applications, and it is not recommended to use it.

Then call () to complete the write.

byte[] buffer = ();
(buffer);
(, "Write successfully",Toast.LENGTH_SHORT).show();

Finally, do some cleaning work, refresh the write-out stream and close the stream.

();
();

2. Read the file

Similarly, first instantiate a FileInputStream.

FileInputStream fiStream = openFileInput(fileName)

Then call ()

int len = ();
byte[] buffer = new byte[len];
(buffer)

Finally, display the text and close the read file stream

(new String(buffer));
(, "Read successfully",Toast.LENGTH_SHORT).show();
();

3. Complete code

import ;
 import ;
 import ;
 import ;
 import ;
 import ;
 import ;
 import ;
 public class MainActivity extends AppCompatActivity {
   private EditText etName;
   private EditText etContent;
   private Button btnWrite;
   private Button btnRead;
   private String fileName = "";
   private String fileContent = "";
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     (savedInstanceState);
     setContentView(.activity_main);
     etName = (EditText)findViewById();
     etContent = (EditText)findViewById();
     btnWrite = (Button)findViewById();
     btnRead = (Button)findViewById();
     (new () {
       @Override
       public void onClick(View view) {
         fileName = ().toString();
         fileContent = ().toString();
         try {
           FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
           byte[] buffer = ();
           (buffer);
           (, "Writing successfully",Toast.LENGTH_SHORT).show();
           ();
           ();
         }catch(Exception e){
           ();
         }
       }
     });
     (new () {
       @Override
       public void onClick(View view) {
         fileName = ().toString();
         try{
           FileInputStream fiStream = openFileInput(fileName);
           int len = ();
           byte[] buffer = new byte[len];
           (buffer);
           (new String(buffer));
           (, "Read successfully",Toast.LENGTH_SHORT).show();
           ();
         }catch(Exception e){
           ();
         }
       }
     });
   }
 }
<RelativeLayout xmlns:andro
   xmlns:tools="/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   <EditText
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:
     android:layout_alignParentTop="true"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true"
     android:text="file name" />
   <EditText
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:
     android:layout_below="@+id/etName"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true"
     android:text="File Content" />
   <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="save"
     android:
     android:layout_alignTop="@+id/btnRead"
     android:layout_toLeftOf="@+id/btnRead"
     android:layout_toStartOf="@+id/btnRead" />
   <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Read"
     android:
     android:layout_below="@+id/etContent"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true" />
 </RelativeLayout>