Android Data Storage - File Storage

file storage


    1. Store the data in a file

        There is an openFileOutPut method in the Context, which can save the data to the specified file. The method receives two parameters, one is the file name, and there is no path, because it is automatically stored in the /data/data/<package name>/files/ directory. The second parameter is the operation mode of the file, mainly two modes MODE_PRIVATE and MODE_APPEND. Among them, MODE_PRIVATE is the default operation mode, which means that when there is the same file name, the written content will overwrite the content in the original file, and MODE_APPEND means that if the file already exists, the content will be appended and a new file will not be created.

        The openFileOutPut method returns a FileOutPutStream object. After getting this object, you can use the Java stream to write data to the file.

public class MainActivity extends AppCompatActivity {
private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.et_IO);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy ();
    String inputText = editText.getText().toString();
    save(inputText);

    }

    public void save(String inputText) {
        FileOutputStream out = null;
        BufferedWriter writer = null;
        try {
            out = openFileOutput("data", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(inputText);
            
        } catch (IOException e) {
            e.printStackTrace ();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }
}

View the saved data.txt file


It can be found that the content entered in EditText has indeed been saved to the file

2. Read data from file

public class MainActivity extends AppCompatActivity {
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.et_IO);
        String inputText = load();
        if (!TextUtils.isEmpty(inputText)){
            editText.setText(inputText);
            editText.setSelection(inputText.length());
            Toast.makeText(this,"Restoring succeeded",Toast.LENGTH_SHORT).show();
        }
    }

    public String load() {
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder content = new StringBuilder();

        try {
            in = openFileInput("data");
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace ();
        } finally {
            if (reader != null) {
                try {
                    reader.close();

                } catch (IOException e) {
                    e.printStackTrace ();
                }
            }
        }
        return content.toString();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325771453&siteId=291194637