背景

在学习今天的数据存储之前我们需要先学习关于文件读取的一些基本概念,才能更好的理解今天学习的内容。

IO流的分类

流向

  • 输入流:读取数据
  • 输出流:写出数据

数据类型

  • 字节流

    字节输入流:读取数据 InputStream

    字节输出流:写出数据 OutputStream

  • 字符流

    字符输入流:读取数据 Reader

    字符输出流:写出数据 Writer

Reader类是Java I/O中读字符的父类,InputStream类是读字节的父类,通过InputStreamReader类将字节转换为字符。

Writer是写字符的父类,OutputStream类是写字节的父类,通过OutputStreamWriter将字符转换为字节。

数据存储到文件中

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
public void saveData(String inputData){
//String data = "采菊东篱下,悠然见南山";
FileOutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;

try {
//openFileOutput()第一个参数是文件名,第二个参数是文件的操作模式,
//MODE_APPEND:表示如果该文件已存在,就往文件里面追加内容。
//MODE_PRIVATE:这是默认的操作模式,表示当指定同样文件名的时候,所写入的内容将会覆盖原文件的内容。
fileOutputStream = openFileOutput("data", Context.MODE_APPEND);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
bufferedWriter.write(inputData);
//Log.e(TAG, "saveData: try");
} catch (IOException e){
e.printStackTrace();
} finally {
try {
if(bufferedWriter != null){
bufferedWriter.close();
Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show();
//Log.e(TAG, "saveData: finally");
}
} catch (IOException e){
e.printStackTrace();
}
}
}

从文件中读取数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public String loadData(){
FileInputStream fileInputStream = null;
BufferedReader bufferedReader = null;
StringBuilder stringBuilder = new StringBuilder();
try {
fileInputStream = openFileInput("data");
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line = "";
while((line = bufferedReader.readLine()) != null){
stringBuilder.append(line);
}
} catch (IOException e){
e.printStackTrace();
} finally {
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
return stringBuilder.toString();
}

实现写入和读取的实例

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.example.study.storage;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.study.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class InputData extends AppCompatActivity {
private EditText input_data;
private Button btn_save;
private static final String TAG = "InputData";
private Button btn_show;
private TextView show_data;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_data);
initView();
}

private void initView() {
input_data = (EditText) findViewById(R.id.input_data);
btn_save = (Button) findViewById(R.id.btn_save);
show_data = (TextView) findViewById(R.id.show_data);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String getInputText = input_data.getText().toString();
saveData(getInputText);
}
});
btn_show = (Button) findViewById(R.id.btn_show);
btn_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String inputText = loadData();
if(!TextUtils.isEmpty(inputText)){
show_data.setText(inputText);
}
}
});
}


public void saveData(String inputData){
//String data = "采菊东篱下,悠然见南山";
FileOutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;

try {
fileOutputStream = openFileOutput("data", Context.MODE_APPEND);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
bufferedWriter.write(inputData);
//Log.e(TAG, "saveData: try");
} catch (IOException e){
e.printStackTrace();
} finally {
try {
if(bufferedWriter != null){
bufferedWriter.close();
Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show();
//Log.e(TAG, "saveData: finally");
}
} catch (IOException e){
e.printStackTrace();
}
}
}


public String loadData(){
FileInputStream fileInputStream = null;
BufferedReader bufferedReader = null;
StringBuilder stringBuilder = new StringBuilder();
try {
fileInputStream = openFileInput("data");
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line = "";
while((line = bufferedReader.readLine()) != null){
stringBuilder.append(line);
}
} catch (IOException e){
e.printStackTrace();
} finally {
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
return stringBuilder.toString();
}
}

通过SharedPreferences存储

1
2
3
4
5
SharedPreferences.Editor editor = getSharedPreferences("user_info", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 21);
editor.putBoolean("married", false);
editor.apply();

从SharePreferences读取数据

1
2
3
4
SharedPreferences data = getSharedPreferences("user_info", MODE_PRIVATE);
String name = data.getString("name", "");
int age = data.getInt("age", 0) ;
boolean married = data.getBoolean("married", false);

实现记住密码的实例

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
71
72
73
74
package com.example.study.storage;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.study.MainActivity;
import com.example.study.R;

public class Login extends AppCompatActivity {
private EditText accountEdit;
private EditText passwordEdit;
private CheckBox rememberPwd;
private Button login;
private SharedPreferences.Editor editor;
private SharedPreferences pref;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
}

private void initView() {
accountEdit = (EditText) findViewById(R.id.account);
passwordEdit = (EditText) findViewById(R.id.password);
rememberPwd = (CheckBox) findViewById(R.id.rember_pwd);
login = (Button) findViewById(R.id.login);
pref = PreferenceManager.getDefaultSharedPreferences(this);
boolean isRemember = pref.getBoolean("remember_password", false);
if(isRemember){
String account = pref.getString("account", "");
String pwd = pref.getString("password", "");
accountEdit.setText(account);
passwordEdit.setText(pwd);
rememberPwd.setChecked(true);
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String account = accountEdit.getText().toString();
String pwd = passwordEdit.getText().toString();
if(account.equals("admin") && pwd.equals("123456")){
editor = pref.edit();
if(rememberPwd.isChecked()){

editor.putBoolean("remember_password", true);
editor.putString("account", account);
editor.putString("password", pwd);

} else {
editor.clear();
}
editor.apply();
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), "account or password is invalid", Toast.LENGTH_LONG).show();
}
}
});
}
}