Filepersistencetest

学习目的:文件存储

  • 将数据保存到文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    public void save(String inputText) {
    FileOutputStream out = null;
    BufferedWriter writer = null;
    try {
    out = openFileOutput("data", Context.MODE_PRIVATE);//MODE_PRIVATE指定同一文件名将被覆盖 openFileOutput()获得FileOutputSteame对象 再借助它构建OutputStreamWriter对象 再使用OutputStreamWriter构建BufferedWriter对象
    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();
    }
    }
    }
    ```

    - 范例
    ```java
    public class MainActivity extends AppCompatActivity {

    private EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edit = (EditText) findViewById(R.id.edit);

    }

    @Override
    protected void onDestroy() {
    super.onDestroy();
    String inputText = edit.getText().toString();
    save(inputText);//掉用自定义方法 存储输入字符
    }

    ```

    - 从文件中读取数据
    ```java
    public String load() {
    FileInputStream in = null;
    BufferedReader reader = null;
    StringBuilder content = new StringBuilder();
    try {
    in = openFileInput("data");//获取FileInputStream对象data
    reader = new BufferedReader(new InputStreamReader(in));构建InputStreamReader对象reader
    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();
    }
  • 范例

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