温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么实现存储与读取

发布时间:2021-07-12 13:58:42 来源:亿速云 阅读:101 作者:Leah 栏目:移动开发

这期内容当中小编将会给大家带来有关Android中怎么实现存储与读取,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

一、数据的保存----系统中

        btn_ok.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String user = et_user.getText().toString().trim();

String pwd = et_pwd.getText().toString().trim();

if(user.isEmpty()||pwd.isEmpty()){

Toast.makeText(getApplicationContext(), "不能为空", 200).show();

}else{

String name="data.txt";

String content=user+"="+pwd;

try {

  FileOutputStream  fos=openFileOutput(name, MODE_PRIVATE);

 

  fos.write(content.getBytes());

  

  fos.close();

  Toast.makeText(getApplicationContext(), "保存成功", 200).show();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

});

    数据的读取----系统中

        btn_show.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String name="data.txt";

try {

FileInputStream  fis=openFileInput(name);

BufferedReader br=new BufferedReader(new InputStreamReader(fis));

//读取文档的数据

String content = br.readLine();

//拆分字符串

String[] strs = content.split("=");

//显示的数据

if(!content.isEmpty()){

et_user.setText(strs[0]);

et_pwd.setText(strs[1]);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

   二、数据保存----内存卡

        File file=new File(Environment.getExternalStorageDirectory(), name);

try {

FileOutputStream  fos=new FileOutputStream(file);

fos.write(content.getBytes());

fos.close();

 Toast.makeText(getApplicationContext(), "保存成功", 200).show();

} catch (Exception e) {

 Toast.makeText(getApplicationContext(), "保存失败", 200).show();

e.printStackTrace();

}

    数据读取----内存卡

        File file=new File(Environment.getExternalStorageDirectory(), name);

try {

FileOutputStream  fos=new FileOutputStream(file);

fos.write(content.getBytes());

fos.close();

 Toast.makeText(getApplicationContext(), "保存成功", 200).show();

} catch (Exception e) {

 Toast.makeText(getApplicationContext(), "保存失败", 200).show();

e.printStackTrace();

}

   三、sharedprefrence (☆☆☆☆

           // 保存

btn_ok.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String user = et_user.getText().toString().trim();

String pwd = et_pwd.getText().toString().trim();

if(user.isEmpty()||pwd.isEmpty()){

Toast.makeText(getApplicationContext(), "不能为空", 200).show();

//获取编辑器

           Editor edit = sp.edit();

//保存数据

           edit.putString("name", user);

           edit.putString("pwd", pwd);

               //提交保存

           edit.commit();

}

}

});

            //读取

        btn_show.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//获取用户名

String user = sp.getString("name", null);

//获取密码

String pwd= sp.getString("pwd", null);

                                //显示

et_user.setText(user);

et_pwd.setText(pwd);

}

});

上述就是小编为大家分享的Android中怎么实现存储与读取了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI