android-data storage internal storage

The following will throw an exception

Save

1. Obtain the output stream (here is append mode)
2. Write data (write data is saved in byte type)
3. Refresh data
4. Close the output stream

 try {
    
    
                    FileOutputStream myFiel = openFileOutput("MyFiel", MODE_APPEND);
                    myFiel.write(et.getText().toString().trim().getBytes());
                    myFiel.flush();
                    myFiel.close();
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }

Read

1. Get the input stream (read the file we saved)
2. Read the data. The
byte type is needed to read the data. Then come one.
3. Close the input stream.
4. Assign the read data to a character variable.
5. Assign a value to the control.

try {
    
    
                    FileInputStream myFile = openFileInput("MyFiel");
                    byte[] by = new byte[myFile.available()];
                    myFile.read(by);
                    myFile.close();
                    String data = new String(by);
                    tv.setText(data);
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }

Guess you like

Origin blog.csdn.net/Willow_Spring/article/details/112999226