Android数据存储之SharePreference和内部存储

Android中的数据存储

*存储方式
1.SharedPreference存储:简单的信息...
2.内部存储:保存在app内部存储空间,非公开
3.外部存储:公共空间
4.数据库存储:结构化数据
5.网络存储:云....

1.SharedPreference的用法

案列效果:点击保存数据会将数据保存data1文件中并显示保存成功且情况编辑框中内容,点击恢复数据则将之前填写的数据恢复到编辑框中

布局文件中:

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="姓名:" />

<EditText
    android:id="@+id/et_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/editbox_background"
    android:hint="请输入姓名" 
    android:textSize="20sp"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:text="年龄:" />

<EditText
    android:id="@+id/et_age"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/editbox_background"
    android:hint="请输入年龄" 
    android:textSize="20sp"
    />
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:orientation="horizontal">
    
    <Button 
        android:id="@+id/btn_save"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FA8072"
        android:text="保存数据"/>
    
     <Button 
        android:id="@+id/btn_resume"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FA8072"
        android:layout_marginLeft="5dp"
        android:text="恢复数据"/>
</LinearLayout>

java代码中;

public class MainActivity extends Activity implements OnClickListener{
    private EditText et_name,et_age;
    private Button btn_save,btn_resume;
    private SharedPreferences sp;//使用键值对的方式存储数据,相当于纸
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btn_save.setOnClickListener(this);
        btn_resume.setOnClickListener(this);
        sp = getSharedPreferences("data1", MODE_PRIVATE);//第一个参数是保存的文件名,第二个是操作模式
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.et_name);
        et_age = (EditText) findViewById(R.id.et_age);
        btn_save = (Button) findViewById(R.id.btn_save);
        btn_resume = (Button) findViewById(R.id.btn_resume);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_save:
            String name = et_name.getText().toString().trim();//获取填写的内容并去掉空格
            String age = et_age.getText().toString().trim();
            if(TextUtils.isEmpty(name)||TextUtils.isEmpty(age)){
                return;    //若填写的内容有一个为空则退出,TextUtils.isEmpty()方法可以一次性进行两种空值的判断,当传入的字符串等于null或者等于空字符串时,该方法都返回true
            }
            Editor edit = sp.edit();//获得编辑器--->笔
            edit.putString("name",name);
            edit.putString("age", age);
            boolean commit = edit.commit();//提交--->保存
            if(commit){
                Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                et_name.setText("");
                et_age.setText("");
            }
            break;
        case R.id.btn_resume:
            String nameValue = sp.getString("name", "");
            String ageValue = sp.getString("age", "");
            et_name.setText(nameValue);
            et_name.setSelection(nameValue.length());//setSelection方法将光标移动到文本末尾以便继续输入
            et_age.setText(ageValue);
            et_age.setSelection(ageValue.length());
            break;
        }   
    }
}

1503569-20181102111512934-1172879298.png

*2.内部存储的用法
主要方法:openFileOutput(文件名,操作模式):返回一个FileOutputStream对象
openFileInput(文件名):返回一个FileInputStream对象
deleteFile(文件名):删除文件
文件保存位置:/data/data/包名/files/...
内部存储特点:内部存储的东西会随着app的卸载而被清掉

案列效果:实现保存,打开,删除文件功能

xml布局:

 <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    
    <EditText 
        android:id="@+id/et_filename"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:drawable/editbox_background"
        android:padding="10dp"
        android:hint="请输入文件名"
        android:textSize="20sp"
        android:singleLine="true"/>
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background_normal"
        android:onClick="savefile"
        android:text="保存"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background_normal"
        android:onClick="openfile"
        android:text="打开"/>
</LinearLayout>

<EditText 
    android:id="@+id/et_content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:hint="请输入内容"
    android:textSize="20sp"
    android:background="@android:drawable/editbox_background"
    />
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="delfile"
    android:text="删除文件"
    android:background="@android:drawable/editbox_background"/>

Java代码中:

public class MainActivity extends Activity {
    private EditText et_filename,et_content;

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

    private void initView() {
        et_filename = (EditText) findViewById(R.id.et_filename);
        et_content = (EditText) findViewById(R.id.et_content);
        
    }
    //保存文件
    public void savefile(View v){
        String filename = et_filename.getText().toString().trim();
        String content = et_content.getText().toString().trim();
        if(TextUtils.isEmpty(filename)||TextUtils.isEmpty(content)){
            return;
        }
        FileOutputStream out=null;
        BufferedWriter bw=null;
        try {
            out = openFileOutput(filename,MODE_PRIVATE);
            bw = new BufferedWriter(new OutputStreamWriter(out));
            bw.write(content);
            et_filename.setText("");
            et_content.setText("");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(bw!=null){
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
        
    }
    //打开文件
    public void openfile(View v){
        String filename = et_filename.getText().toString().trim();
        if(TextUtils.isEmpty(filename)){
            return;
        }
        FileInputStream in=null;
        BufferedReader br=null;
        StringBuilder content = new StringBuilder();
        try {
            in = openFileInput(filename);
            br = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while((line = br.readLine())!=null ){
                content.append(line);
            }
            et_content.setText(content);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(br!=null){
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }       
    }
    //删除文件
    public void delfile(View v){
        String filename = et_filename.getText().toString().trim();
        if(TextUtils.isEmpty(filename)){
            return;
        }
        boolean deleteFile = deleteFile(filename);
        if(deleteFile){
            Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
            et_filename.setText("");
            et_content.setText("");
        }       
    }
}

1503569-20181102163017571-135010267.png

转载于:https://www.cnblogs.com/SanguineBoy/p/9895182.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值