ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android中关于IO储存的小记

2020-06-26 23:09:37  阅读:215  来源: 互联网

标签:username Toast String mrsoft IO new Android password 小记


通过Shared Preferences存储数据

 private SharedPreferences mrsoft;//创建对象

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qq2);
        final EditText qqzh = findViewById(R.id.qqzh);
        final EditText qqmm = findViewById(R.id.qqmm);
        Button qqdl=findViewById(R.id.qqdl);


        mrsoft = getSharedPreferences("mrsoft", MODE_PRIVATE);
       String username= mrsoft.getString("username",null);//获取账号,默认值设为mr
        String password= mrsoft.getString("password",null);
        if(username!=null && password!=null)
        {
            qqzh.setText(username);
            qqmm.setText(password);

           // if (username.equals(mr)&&password.equals(mrsofts)) {
             //   Intent intent = new Intent(qq2.this, duo.class);
              //  startActivity(intent);
            //}
        }

 qqdl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String in_username = qqzh.getText().toString();
                    String in_password = qqmm.getText().toString();
                    SharedPreferences.Editor editor = mrsoft.edit();
                    //if (in_username.equals(mr) && in_password.equals(mrsofts)) {
                    editor.putString("username1", in_username);
                    editor.putString("password1", in_password);
                    Log.i("调试是否获取到", "执行到此");

                    Intent intent = new Intent(qq2.this, duo.class);
                    startActivity(intent);
                    Toast.makeText(qq2.this, "已保存密码", Toast.LENGTH_SHORT).show();

                    editor.commit();
                    //}
                    //else {
                    //  Toast.makeText(qq2.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
                    //}

                }
            });
        }

总结 对于Shared Preferences来说他出存储的位置在data/data目录下手机无法直接查看,而且以xml文件的方式存储到本地;xml文件如下所示

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="specialtext">hajsdh><?//</string>
<string name="username">dsa</string>
<string name="password">dasdasd</string>
<int name="int" value="47" />
<boolean name="or" value="true" />
</map>

储存的步骤:
1.通过mrsoft = getSharedPreferences("mrsoft", MODE_PRIVATE);方法先获取到一个sharp对象
2.通过SharedPreferences.Editor editor = mrsoft.edit();获取到sharp的一个内部对象editor才能进行存储操作
3.直接put即可,有点类似map集合前key后 valueeditor.putString("username1", in_username); editor.putString("password1", in_password);

读取的操作:
1.通过mrsoft = getSharedPreferences("mrsoft", MODE_PRIVATE);方法先获取到一个sharp对象
2.这样即可String username= mrsoft.getString("username",null);//获取账号,默认值设为mr String password= mrsoft.getString("password",null);

通过IO流对象存储读取对象

public class jishi extends AppCompatActivity {

   byte[] buffer=null;
    File file;
    private EditText jis;
    private FileInputStream fis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jishi);
        Button baochun = findViewById(R.id.baochun);
        jis = findViewById(R.id.ji);
        file=new File(Environment.getExternalStorageDirectory(),"new.txt");

        try {
            fis = new FileInputStream(file);
            buffer =new byte[fis.available()];
            fis.read(buffer);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                fis.close();
                String data=new String(buffer);
                jis.setText(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        baochun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FileOutputStream fos=null;
                String text= jis.getText().toString();

                try {
                    fos=new FileOutputStream(file);
                    fos.write(text.getBytes());
                    fos.flush();
                    Toast.makeText(jishi.this,"保存成功",Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(jishi.this,"文件不存在异常",Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(jishi.this,"io异常",Toast.LENGTH_SHORT).show();
                }finally {
                    if(fos!=null)
                    {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }


            }
        });
    }


}

创建io对象:然后获取读取没什么区别,就是读取流中I有个

   fis = new FileInputStream(file);
            buffer =new byte[fis.available()];
            fis.read(buffer);

看这个片段,不再通过指定byte【1024】来指定了

标签:username,Toast,String,mrsoft,IO,new,Android,password,小记
来源: https://blog.csdn.net/qq_45541497/article/details/106952536

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有