SharedPreferences(数据存储)

简单实例

主程序

package com.test.dukang.mysharedpreferrence;

import android.app.Activity;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener{
    private Button mButton_write;
    private Button mButton_read;

    private EditText mEditText;
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton_read= (Button) findViewById(R.id.button_read);
        mButton_write= (Button) findViewById(R.id.button_write);
        mEditText= (EditText) findViewById(R.id.editText);
        mTextView= (TextView) findViewById(R.id.textView);
        mButton_read.setOnClickListener(this);
        mButton_write.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_read:
                //也可以用getPreferences不用取文件名,默认使用类的名作为文件名
                SharedPreferences sharedPreferences=getSharedPreferences("nihao",MODE_PRIVATE);
                String content=sharedPreferences.getString("hao","keng");
                mTextView.setText(content);
                break;
            case R.id.button_write:
                write();
                break;
        }
    }

    private void write() {
        //也可以用getPreferences不用取文件名,默认使用类的名作为文件名
        SharedPreferences sharedPreferences=getSharedPreferences("nihao",MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("hao",mEditText.getText().toString());
        editor.commit();
    }
}

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:orientation="vertical">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="nihao"/>
    <Button
        android:id="@+id/button_write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入数据"/>
    <Button
        android:id="@+id/button_read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取数据"/>

</LinearLayout>

效果图

这里写图片描述

数据存储集锦

主程序

package com.test.dukang.mysharedpreferrence;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Printer;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class MainActivity extends Activity implements View.OnClickListener {
    private Button mButton_write;
    private Button mButton_read;
    private Button mButtonCacheWrite;
    private Button mButtonCacheRead;
    private Button mButtonCacheWriteDir;
    private Button mButtonCacheReadDir;
    private Button mButtonCacheWriteSd;
    private Button mButtonCacheReadSd;
    private EditText mEditText;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton_read = (Button) findViewById(R.id.button_read);
        mButton_write = (Button) findViewById(R.id.button_write);
        mButtonCacheWrite = (Button) findViewById(R.id.button_cache_write);
        mButtonCacheRead = (Button) findViewById(R.id.button_cache_read);
        mButtonCacheWriteDir = (Button) findViewById(R.id.button_cache_write_dir);
        mButtonCacheReadDir = (Button) findViewById(R.id.button_cache_read_dir);
        mButtonCacheWriteSd = (Button) findViewById(R.id.button_cache_write_sd);
        mButtonCacheReadSd = (Button) findViewById(R.id.button_cache_read_sd);
        mEditText = (EditText) findViewById(R.id.editText);
        mTextView = (TextView) findViewById(R.id.textView);
        mButton_read.setOnClickListener(this);
        mButton_write.setOnClickListener(this);
        mButtonCacheWrite.setOnClickListener(this);
        mButtonCacheRead.setOnClickListener(this);
        mButtonCacheWriteDir.setOnClickListener(this);
        mButtonCacheReadDir.setOnClickListener(this);
        mButtonCacheWriteSd.setOnClickListener(this);
        mButtonCacheReadSd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_read:
                                SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
                String content = sharedPreferences.getString("hao", "keng");
                mTextView.setText(content);
                break;
            case R.id.button_write:
                //会在包下,创建的files文件夹下创建一个文件
                write();
                break;
            case R.id.button_cache_write:
                //同样是在files下,但不能自己定义文件名,文件名即为所在类名
                FileOutputStream fileOutputStream = null;
                try {
                    fileOutputStream = openFileOutput("data", MODE_PRIVATE);
                    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(fileOutputStream));
                    printWriter.write("你好");
                    printWriter.flush();
                    printWriter.close();
                    fileOutputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button_cache_read:
                try {
                    FileInputStream fis = openFileInput("data");
                    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                    String line = br.readLine();
                    while (line != null) {
                        Log.d("share", line);
                        line = br.readLine();
                    }
                    br.close();
                    fis.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button_cache_write_dir:
                //向cache文件夹下写入文件,缓存文件可设定自定义条件自动删除
                //前面为文件夹路径名,后面为文件名
                writeDir();

                break;
            case R.id.button_cache_read_dir:
                //读取文件参考上面的读文件,直接在里面寻找

                break;
            case R.id.button_cache_write_sd:
                //需要添加权限
                File file1 = new File(Environment.getExternalStorageDirectory(), "nimei.txt");
                if (!file1.exists()) {
                    try {
                        file1.createNewFile();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file1);
                    //也可以用fos.write("你好,你妹".getBytes());来直接以字节的形式写入
                    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(fos));
                    printWriter.write("你好,你妹。");
                    printWriter.flush();
                    printWriter.close();

                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button_cache_read_sd:

                break;
        }
    }


    private void writeDir() {
        File file = new File(getCacheDir(), "dukang");
        if (!file.exists()) {
            try {
                file.createNewFile();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(fos));
            printWriter.write("你好");
            printWriter.flush();
            printWriter.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void write() {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        ;
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("hao", mEditText.getText().toString());
        editor.commit();
    }
}

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:orientation="vertical">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="nihao"/>
    <Button
        android:id="@+id/button_write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入数据"/>
    <Button
        android:id="@+id/button_read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取数据"/>
    <Button
        android:id="@+id/button_cache_write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写缓存"/>
    <Button
        android:id="@+id/button_cache_read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读缓存"/>
    <Button
        android:id="@+id/button_cache_write_dir"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写cache缓存"/>
    <Button
        android:id="@+id/button_cache_read_dir"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读cache缓存"/>
    <Button
        android:id="@+id/button_cache_write_sd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写sd缓存"/>
    <Button
        android:id="@+id/button_cache_read_sd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读sd缓存"/>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值