File storage of data storage

This week the company has nothing to do, nothing to do! I went over the basics of data storage again.
The effect of writing is that the first time you input data on editText, when you destroy it and save it, then when you come in the second time, you read the previous data and display it! Go to the code directly. There are comments on the code

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText = (EditText) findViewById(R.id.edit);
        String load = load();
        if(!TextUtils.isEmpty(load)) {
            mEditText.setText(load);
            /**
             * Move the cursor to the end to continue input*/
            mEditText.setSelection(mEditText.length());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Save data
        save(mEditText.getText().toString());
    }

    //Save data
    private void save(String data) {
        FileOutputStream out = null;
        BufferedWriter writer = null;
        try {
            /**
             * "dataFile" Represents the name of the file where the data is saved (when reading data, it is also read according to the file name), and
             *    The default path of file storage is under / data / data / < packagename > / file
             * Context.MODE_APPEND Indicates that if the file name exists, the content will be appended to it. If it is not pure, a new file will be created
             * Context.MODE_PRIVATE Indicates that when the same file name is specified, the contents written will overwrite the contents of the original file
             *
             * To view the files, click Tools > Android > Android device monitor in the as navigation bar
             *      And under the File Explorer tab, you can find it according to the previous path. Finally, you can view it after exporting it to the computer
             * */
            out = openFileOutput("dataFile", Context.MODE_APPEND);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (writer != null){
                    writer.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    //Read data
    private String load(){
        FileInputStream inputStream = null;
        BufferedReader bufferedReader = null;
        StringBuilder builder = new StringBuilder();
        try {
            inputStream = openFileInput("dataFile");
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line ;
            while ((line =bufferedReader.readLine())!= null ){
                builder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if(bufferedReader != null) {
                    bufferedReader.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return builder.toString();
    }
}

Above:

Keywords: Android

Added by koughman on Wed, 01 Apr 2020 17:10:04 +0300