文件存储
方法存储是Android最基本的一种数据存储方式下面就给大家承上代码:
package com.example.liangshaoteng.fileoutputstream;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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 MainActivity extends AppCompatActivity {
private BufferedWriter bufferedWriter;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.et_text);
}
private void load() {
try {
//从代码的openFileInput我们就能看出是:打开文件从里面输入到什么地方
FileInputStream data = openFileInput("dataSave");
StringBuffer stringBuffer = new StringBuffer();
//缓冲区读取
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(data));
String line = "";
//一行一行的读取
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
//通过StringBuffer追加到一起Toast出来
Toast.makeText(getApplicationContext(), stringBuffer.toString(), Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void save(String inputText) {
if (inputText.isEmpty()) return;
try {
//从代码的openFileOutput我们就能看出是:打开文件从外面放东西
//第一个参数是:文件的名称 第二个参数是:存储的模式
//存储模式 :
//Context.MODE_PRIVATE:别的应用不能访问得到SharedPreferences对象
//Context.MODE_WORLD_READABLE:别的应用可以访问,并且是可以读取SharedPreferences中的数据,但不能写入数据
//Context.MODE_WORLD_WRITEABLE:别的应用可以访问,可以在SharedPreferences中写入修改数据
FileOutputStream dataSave = openFileOutput("dataSave", Context.MODE_PRIVATE);
//在放东西的期间是需要一个缓冲BufferedWriter
bufferedWriter = new BufferedWriter(new OutputStreamWriter(dataSave));
//然后通过缓冲写入数据
bufferedWriter.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
Toast.makeText(getApplicationContext(), "保存成功!", Toast.LENGTH_LONG).show();
if (bufferedWriter != null) {
try {
//不要忘记用完要关闭
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void commit(View view) {
String trim = editText.getText().toString().trim();
if (trim.isEmpty()) return;
save(trim);
}
public void input(View view) {
load();
}
}
下面就是ShardPreference这一个非常简单所以我就不再详细的讲解了后续呢我将给出工具类大家可以下载直接拿来用
大家先看一下工具类ShardPreference
package com.waywings.fm.utils;
import android.content.Context;
import android.content.SharedPreferences;
public class ShareUtil {
public static String CONFIG = "config";
public static String FCONFIG = "fmconfig";
public static String INTFIG = "intconfig";
private static SharedPreferences sharedPreferences;
public static void saveFloatData(Context context, String key, Float value) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(FCONFIG, Context.MODE_PRIVATE);
}
sharedPreferences.edit().putFloat(key, value).commit();
}
public static Float getFloatData(Context context, String key, Float defValue) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(FCONFIG, Context.MODE_PRIVATE);
}
return sharedPreferences.getFloat(key, defValue);
}
public static void saveStringData(Context context, String key, String value) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
}
sharedPreferences.edit().putString(key, value).commit();
}
public static String getStringData(Context context, String key, String defValue) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
}
return sharedPreferences.getString(key, defValue);
}
public static int getIntData(Context context, String key, int defValue) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(INTFIG, Context.MODE_PRIVATE);
}
return sharedPreferences.getInt(key, defValue);
}
public static void savaIntData(Context context, String key, int value) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(INTFIG, Context.MODE_PRIVATE);
}
sharedPreferences.edit().putInt(key, value).commit();
}
}
不足之处还望大家指出 !
Sqlite存储 : 请大家移步到我的另一篇文章 :点击打开链接
后续给大家提供的工具类的下载 : 工具类下载