1:文件
2:SharedPreference
3:数据库
文件方面需要理解Java流的知识:
Java流可以理解为:虚拟的存储容器
所以在操作流的时候一定要先实例化流,再打开流,再建立流的读写工具。写入相关代码如下:
public void save(String inputText)
{
FileOutputStream out=null;
BufferedWriter write=null;
try{
out=openFileOutput("data", Context.MODE_PRIVATE);
write=new BufferedWriter(new OutputStreamWriter(out));
write.write(inputText);
}catch(IOException e)
{
e.printStackTrace();
}finally {
try {
if(write!=null)
{write.close();}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
读取相关代码如下:
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(reader!=null)
{
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return content.toString();
}
SharedPreferences:
三种方法实例化:
Context .getSharedPreferences()
Activity.getPreferences()
PreferenceManager.getDefaultSharedPreferences()
存储代码如下:
SharedPreferences.Editor editor=getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "ZPY");
editor.putInt("age", 20);
editor.putBoolean("married", false);
editor.commit();
读取代码如下:
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("AA", "name is"+name);
Log.d("AA", "age is "+age);
Log.d("AA", "married id "+married);
数据库存储:SQLite:
(1)继承SQLiteOpenHelper(),创建自己的类
(2)重写OnCreate()和OnUpgrade()方法
(3)实例化SQLiteOpenHelper
(4)通过getWritableDatabase()或者getReadableDatabase()得到数据库对象
(5)通过DB提供的insert,delete,update,query,进行增删改查。但是个人觉得直接使用SQL进行查询比较方便。相关的增删改运行SQL的方法为:db.exec(SQL);查询运行方法有所不同,是db.rawQuery(SQL);
package com.example.databasetest;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private MyDatabaseHelper dbHelper;
private Button createDB;
private Button addData,updateData,deleteData,queryData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new MyDatabaseHelper(this, "BookStore.db", null, 2);
createDB=(Button) findViewById(R.id.create_database);
createDB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dbHelper.getWritableDatabase();
}
});
addData=(Button) findViewById(R.id.add_data);
addData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name", "The Da Vinci Code");
values.put("author", "Dan Brown");
values.put("pages", 499);
values.put("price",16.96);
db.insert("Book", null,values);
values.clear();
values.put("name", "AAA");
values.put("author","ZPY");
values.put("pages",123);
values.put("price", 13.14);
db.insert("Book", null, values);
}
});
updateData=(Button) findViewById(R.id.update_data);
updateData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("price", 52.13);
db.update("Book", values, "name=?", new String[]{"AAA"});
}
});
deleteData=(Button) findViewById(R.id.delete_data);
deleteData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbHelper.getWritableDatabase();
db.delete("Book", "name=?", new String[]{"AAA"});
}
});
queryData=(Button) findViewById(R.id.query_data);
queryData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbHelper.getWritableDatabase();
Cursor cursor=db.query("Book", null, null, null, null, null, null);
if(cursor.moveToFirst())
{
do{
String name =cursor.getString(cursor.getColumnIndex("name"));
String author=cursor.getString(cursor.getColumnIndex("author"));
int pages=cursor.getInt(cursor.getColumnIndex("pages"));
double price=cursor.getDouble(cursor.getColumnIndex("price"));
Log.d("DB",name+" "+author+" "+pages+" "+price);
}while(cursor.moveToNext());
}
cursor.close();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}