数据存储

 

1. 文件存储

package com.example.zhuopeng.demo1;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
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.OutputStreamWriter;

public class FileDemoActivity extends AppCompatActivity {
    private EditText editText;
    private EditText editText1;
    private static final String TAG = "FileDemoActivity";
    public static void actionStart(Context context){
        Intent intent = new Intent(context,FileDemoActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_demo);
        Log.d(TAG,"FileDemoActivity is created ");
        editText = (EditText)findViewById(R.id.editText2);
        editText1 = (EditText)findViewById(R.id.editText);
    }

    public void storageFile(View view){
       save();

       String myload = load();
       if(!TextUtils.isEmpty(myload)){
           editText1.setText(myload);
           //将光标移动到输入的末尾
           editText1.setSelection(myload.length());
           Toast.makeText(this,"load",Toast.LENGTH_SHORT);
       }
    }
    private String  load(){
        FileInputStream fileInputStream = null;
        BufferedReader bufferedReader = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            fileInputStream = openFileInput("b.java");
            bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            String line = "";
            while((line = bufferedReader.readLine())!=null){
                stringBuilder.append(line);
            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader != null){
                try{
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stringBuilder.toString();
    }
    private void save(){

        //简单的数据存储6
        String data = editText.getText().toString();
        Log.d(TAG,"data is " + data);
        FileOutputStream out = null;
        BufferedWriter writer = null;

        try{
            out = openFileOutput("b.java",Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(data);
            Log.d(TAG,"storeFile button");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if(writer != null){
                    writer.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

}

2.sharedPreference存储

写入

 public void sharedPreference(View view){
        SharedPreferences.Editor editor = getSharedPreferences("sharedPrerenceData",MODE_PRIVATE).edit();
        editor.putString("name","zhuopeng");
        editor.putInt("age",21);
        editor.apply();
        Log.d(TAG,"sharedPreference ");
    }

 读取

 //sharedPreference读取数据
    public void sharedPreferenceRestore(View view){
        SharedPreferences sharedPreferences = getSharedPreferences("sharedPrerenceData",MODE_PRIVATE);
        String name = sharedPreferences.getString("name","");
        int age = sharedPreferences.getInt("age",0);
        Log.d(TAG,"name is :" + name);
        Log.d(TAG,"age is " +age);
    }

数据库

 

posted @ 2018-02-11 11:16  式微胡不归  阅读(120)  评论(0编辑  收藏  举报