(Andorid基础)通过内部存储保存数据于文件案例

通过内部存储读写文件 ,从而保存数据,但是 不要要保存大量

万事页面始

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    android:background="#E6E6E6"
    >

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/ic_launcher_foreground"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号"
            android:id="@+id/tv_username"
            android:textColor="#000"
            android:textSize="20sp"
            android:padding="10dp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_username"
            android:background="@null"
            android:padding="10dp"
            android:layout_marginLeft="5dp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/white"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:id="@+id/tv_password"
            android:textColor="#000"
            android:textSize="20sp"
            android:padding="10dp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_password"
            android:background="@null"
            android:padding="10dp"
            android:layout_marginLeft="5dp"
            />
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:id="@+id/btn_login"
        android:layout_marginTop="35dp"
        android:background="@android:color/holo_green_light"
        />
</LinearLayout>

在这里插入图片描述

创建数据存储工具类FileSaveQQ :

public class FileSaveQQ {
    //保存账号密码的方法
    public static boolean save(Context context , String username , String  password){
        //获得输出流
        FileOutputStream fileOutputStream = null;

        try {
            //获取文件的输出流
            fileOutputStream = context.openFileOutput("data.txt",Context.MODE_PRIVATE);
            fileOutputStream.write((username+":"+password).getBytes());
            return true;
        } catch (FileNotFoundException e) {
            Log.e("文件操作异常","data.txt文件没找到!");
            return false;
        } catch (IOException e) {
            Log.e("文件操作异常","写文件失败");
            return false;
        }finally {
            try {
                if (fileOutputStream!=null){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                Log.e("文件操作异常","关闭文件输出流失败!");
            }
        }
    }
    //获取已经保存的账号密码
    public static Map<String ,String > getUserAccount(Context context){
        //定义临时变量content
        String  content = "";
        FileInputStream fileInputStream = null;
        try {
             fileInputStream = context.openFileInput("data.txt");

             //读之前创建字节缓存区
            byte[] buffer = new byte[fileInputStream.available()];
            fileInputStream.read(buffer);
            content = new String(buffer);//将缓存的字节数组构造字符串对象
            Map<String ,String > userMap = new HashMap<>();
            String[] infos = content.split(":");  //第一个是账号  ,第二个是密码
            userMap.put("username" , infos[0]);
            userMap.put("password" , infos[1]);

            return userMap;
        } catch (FileNotFoundException e) {
            Log.e("文件操作失败" , "找不到data.txt文件");
            return null;
        } catch (IOException e) {
            Log.e("文件操作失败" , "开启缓存失败");
            return null;
        }finally {
            if (fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    Log.e("文件操作失败" , "关闭文件输入流失败!");
                }

            }

        }
    }
}

编写Activity类:

没啥好说的
初始化页面 继承监听接口设置按钮监听调用静态方法 然后Toast提示

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    //两个输入框,一个按钮
    private Button button;
    private EditText ed_username , ed_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //获取保存的账号密码
        Map<String , String >  userInfo  = FileSaveQQ.getUserAccount(this);
        if (userInfo!=null){
            ed_password.setText(userInfo.get("password"));
            ed_username.setText(userInfo.get("username"));
        }
    }

    private void  initView(){
        ed_password = (EditText) findViewById(R.id.et_password);
        ed_username = (EditText)findViewById(R.id.et_username);
        button = findViewById(R.id.btn_login);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //获取页面上的账号密码
        String username = ed_username.getText().toString().trim();//去除前后空格
        String password = ed_password.getText().toString().trim();//去除前后空格
        //判断空值
        if (TextUtils.isEmpty(username)){
            Toast.makeText(this,"请输入用户名" , Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)){
            Toast.makeText(this,"请输入密码" , Toast.LENGTH_SHORT).show();
            return;
        }
        //保存信息
        boolean save = FileSaveQQ.save(this, username, password);
        if (save){
            Toast.makeText(this,"登录成功,您的信息已经保存!" , Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"登录成功,但您的信息保存失败!" , Toast.LENGTH_SHORT).show();
        }

    }
}

改造

创建文件真的繁琐,下面是SharedPreferences存储

页面不变:

就修改工具类:

public class FileSaveQQ {
    //保存账号密码的方法
    public static boolean save(Context context , String username , String  password){
       //获取对象
        SharedPreferences sharedPreferences = context.getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("username" , username);
        edit.putString("password" , password);
        edit.apply();
        return true;

    }
    //获取已经保存的账号密码
    public static Map<String ,String > getUserAccount(Context context){
        //获取对象
        SharedPreferences sharedPreferences = context.getSharedPreferences("data",Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", null);
        String password = sharedPreferences.getString("password", null);
        Map<String  , String  > map = new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        return map;
    }
}

ps:书本总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值