Android_内部文件读取

我们这里做一个安卓的简易的文件读取程序之记住密码

  • 首先我们先明确安卓的存储路径, 所有安装至手机的应用都会在 data/data 目录下生成一个安卓文件夹(包名),这个文件夹就是安卓存储的路径
  • 在运行安卓程序时,按home键 不会销毁 Activity,但是返回键 会销毁 Activity

首先我们写前台代码,一个简单的登录页面,两个EditText,一个CheckBox记住密码,以及登录按钮Button

这里我们只是注重于内部存储,而没有去处理登录的业务逻辑

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:app="http://schemas.android.com/apk/res-auto"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context="com.example.rwinRom.MainActivity">
 8 
 9     <EditText
10         android:id="@+id/et_name"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:hint="请输入账号" />
14 
15     <EditText
16         android:id="@+id/et_pass"
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:paddingTop="20dp"
20         android:hint="请输入密码" />
21 
22     <RelativeLayout
23         android:layout_width="match_parent"
24         android:layout_height="wrap_content">
25 
26         <CheckBox
27             android:id="@+id/checkbox1"
28             android:layout_width="wrap_content"
29             android:layout_height="wrap_content"
30             android:layout_centerVertical="true"
31             android:text="记住账号和密码" />
32 
33         <Button
34             android:layout_width="wrap_content"
35             android:layout_height="wrap_content"
36             android:layout_alignParentRight="true"
37             android:onClick="login"
38             android:text="登录" />
39 
40     </RelativeLayout>
41 
42 </LinearLayout>

然后是后台的处理,获取前台输入的信息以及是否勾选记住密码

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7 
 8         readAccount();
 9     }
10 
11     public void readAccount(){
12         //读取文件,回显数据
13         File file = new File("data/data/com.example.rwinRom/info.txt");
14 
15         if (file.exists()){
16             try{
17                 FileInputStream fis = new FileInputStream(file);
18                 //把字节流转换为字符流
19                 BufferedReader br = new BufferedReader(new InputStreamReader(fis));
20 
21                 //读取文件的文本
22                 String text = br.readLine();
23                 String s[] = text.split("&&");
24 
25                 EditText et_name = (EditText)findViewById(R.id.et_name);
26                 EditText et_pass = (EditText)findViewById(R.id.et_pass);
27 
28                 //给输入框设置文本
29                 et_name.setText(s[0]);
30                 et_pass.setText(s[1]);
31 
32             }catch(Exception e){
33                 e.printStackTrace();
34             }
35         }
36 
37     }
38 
39     public void login(View v){
40         EditText et_name = (EditText)findViewById(R.id.et_name);
41         EditText et_pass = (EditText)findViewById(R.id.et_pass);
42         //获取用户输入的账号密码
43         String name = et_name.getText().toString();
44         String pass = et_pass.getText().toString();
45 
46         CheckBox cb = (CheckBox)findViewById(R.id.checkbox1);
47         //判断选框是否选中
48         File file = new File("data/data/com.example.rwinRom/info.txt");
49         if(cb.isChecked()){
50             try{
51                 FileOutputStream fos = new FileOutputStream(file);
52                 fos.write((name + "&&" + pass).getBytes());
53                 fos.close();
54             }catch(Exception e){
55                 e.printStackTrace();
56             }
57         };
58 
59         Log.i("gogogo", "登录成功");
60     }
61 }

还有就是,这么去new一个File对象非常不方便而且容易写错,我们可以调用文件的api接口

  • getFilesDir    返回路径 data/data/包名/files
  •  getCacheDir 返回路径 data/data/包名/cache
  •  区别不仅仅在路径,如果内部存储空间满了,cache中的文件会 被删除


使用方法就是

File file = new File(getFilesDir(), "info.txt");
原文地址:https://www.cnblogs.com/AganRun/p/7802284.html