There are several ways to store Android data

  • 2020-10-31 21:59:40
  • OfStack

Here are five ways to store Android data:

1, SharedPreferences

2. File storage

3. SQLite database

4, ContentProvider

5. Network storage

This article focuses on using files to store data. The Android file operation USES the FileOutputStream and FileInputStream classes in Java.IO.

1. Store files

First instantiate 1 FileOutputStream.

FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
// fileName: The name of the file to write to
// MODE_PRIVATE: Is the default mode of operation, which means that the file is private data and can only be accessed by the application itself. In this mode, the contents written will overwrite the contents of the original file
// MODE_APPEND: The schema checks if a file exists, appends it to the file, or creates a new file.
// MODE_WORLD_READABLE: indicates that the current file can be read by other applications and is not recommended
// MODE_WORLD_WRITEABLE: Indicates that the current file can be written by other applications and is not recommended

The write is then completed by calling foStream.write ().

byte[] buffer = fileContent.getBytes();
foStream.write(buffer);
Toast.makeText (ES58en.this, "write successfully ", Toast.LENGTH_ES62en).show ();

Finally, do some cleanup, flush out the write stream and close the stream.

foStream.flush();
foStream.close();

2. Read the file

Similarly, first instantiate 1 FileInputStream.

FileInputStream fiStream = openFileInput(fileName)

Then call fiStream.read ()

int len = fiStream.available();
byte[] buffer = new byte[len];
fiStream.read(buffer)

Finally, the text is displayed and the read file stream is closed

etContent.setText(new String(buffer));
Toast.makeText(MainActivity.this, "read successfully ",Toast.LENGTH_SHORT).show();
fiStream.close();

3. Complete code


import android.support.v.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 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) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     etName = (EditText)findViewById(R.id.etName);
     etContent = (EditText)findViewById(R.id.etContent);
     btnWrite = (Button)findViewById(R.id.btnWrite);
     btnRead = (Button)findViewById(R.id.btnRead);
     btnWrite.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
         fileName = etName.getText().toString();
         fileContent = etContent.getText().toString();
         try {
           FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
           byte[] buffer = fileContent.getBytes();
           foStream.write(buffer);
           Toast.makeText(MainActivity.this, " Write to successful ",Toast.LENGTH_SHORT).show();
           foStream.flush();
           foStream.close();
         }catch(Exception e){
           e.printStackTrace();
         }
       }
     });
     btnRead.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
         fileName = etName.getText().toString();
         try{
           FileInputStream fiStream = openFileInput(fileName);
           int len = fiStream.available();
           byte[] buffer = new byte[len];
           fiStream.read(buffer);
           etContent.setText(new String(buffer));
           Toast.makeText(MainActivity.this, " Read the success ",Toast.LENGTH_SHORT).show();
           fiStream.close();
         }catch(Exception e){
           e.printStackTrace();
         }
       }
     });
   }
 }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/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:id="@+id/etName"
     android:layout_alignParentTop="true"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true"
     android:text=" The file name " />
   <EditText
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/etContent"
     android:layout_below="@+id/etName"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true"
     android:text=" The file content " />
   <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text=" save "
     android:id="@+id/btnWrite"
     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:id="@+id/btnRead"
     android:layout_below="@+id/etContent"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true" />
 </RelativeLayout>


Related articles: