安卓学习-(数据存储(文件和SharedPreferences))

1、方式1:使用文件存储
package com.example.filepersistencetest;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {

private EditText edit ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
edit = (EditText) findViewById(R.id. edit );
String inputText = load();
//!TextUtils.isEmpty() 用于判断一个数据是不是 null
if (!TextUtils. isEmpty (inputText)) {
edit .setText(inputText);
edit .setSelection(inputText.length());
Toast. makeText ( this , "Restoring succeeded" , Toast. LENGTH_SHORT ).show();
}
}

@Override
protected void onDestroy() {
super .onDestroy();
String inputText = edit .getText().toString();
save(inputText);
}

//从文件中把数据读出来,文件路径是确定的,我们只需要指定一个文件名字就可以读取内容
public void save(String inputText) {
FileOutputStream out = null ;
BufferedWriter writer = null ;
try {
// Context.MODE_PRIVATE :默认模式,文件同名会覆盖数据
//MODE_APPEND:表示追加模式
out = openFileOutput( "data" , Context. MODE_PRIVATE );
writer = new BufferedWriter( new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null ) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

/ /把数据保存到文件中,路径是默认的,我们只可以指定一个文件名字,这里给的是data这个名字
public String load() {
FileInputStream in = null ;
BufferedReader reader = null ;
StringBuilder content = new StringBuilder();
try {
in = openFileInput( "data" );
reader = new BufferedReader( new InputStreamReader(in));
String line = "" ;
while ((line = reader.readLine()) != null ) {
content.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null ) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
}

2、方式2:使用SharedPreferences(python单层字典)
package com.example.sharedpreferencestest;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
Button saveData = (Button) findViewById(R.id. save_data );
saveData.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
/ /getSharedPreferences 是在context类中获取 SharedPreferences 对象的方法。
//需要传入文件名(这里是data), 第二个参数 MODE_PRIVATE表示只有当前程序可以操作数据
SharedPreferences.Editor editor = getSharedPreferences( "data" , MODE_PRIVATE ).edit();
editor.putString( "name" , "Tom" );
editor.putInt( "age" , 28 );
editor.putBoolean( "married" , false );
editor.apply();
}
});
Button restoreData = (Button) findViewById(R.id. restore_data );
//这里是取出数据
restoreData.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences pref = getSharedPreferences( "data" , MODE_PRIVATE );
String name = pref.getString( "name" , "" );
int age = pref.getInt( "age" , 0 );
boolean married = pref.getBoolean( "married" , false );
Log. d ( "MainActivity" , "name is " + name);
Log. d ( "MainActivity" , "age is " + age);
Log. d ( "MainActivity" , "married is " + married);
}
});
}
}

































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值