Android数据存储

版权声明:本文为Mr.release原创文章,未经博主允许不得转载。如有问题请关注https://blog.csdn.net/AliEnCheng/article/details/78444535

Android数据存储

1.保存数据到data

//保存数据到data
	public void save2data(View view) throws Exception{
		
		//data/data/包名/files/a.txt
		//写文件
		FileOutputStream fos = openFileOutput("a.txt", MODE_PRIVATE);
		fos.write("小明".getBytes());
		fos.close();
		
		//读文件
		FileInputStream fis = openFileInput("a.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(fis));
		String line = br.readLine();
		Toast.makeText(this, line, Toast.LENGTH_SHORT).show();
		fis.close();
		
		//删除文件
		deleteFile("a.txt");
	}

2.保存数据到sdcard

//保存数据到sdcard
	public void save2sdcard(View view) throws Exception{
	
		//data/data/包名//a.txt
		String state = Environment.getExternalStorageState();
		if(Environment.MEDIA_MOUNTED.equals(state)){
			File dir = Environment.getExternalStorageDirectory();
			File file = new File(dir,"b.txt");
			FileWriter fw = new FileWriter(file);
			
			fw.write("小明名");
			fw.close();
		}
	}

3.存储到SharedPreferences

//存储到SharedPreferences
	public void save2sp(View view) throws Exception{
		// /data/data/包名/shared_prefs/c.xml
		SharedPreferences sp = getSharedPreferences("c", MODE_PRIVATE);
		
		//保存数据
		sp.edit().putString("name", "小明").commit();
		//获取数据
		String string = sp.getString("name", "提示默认值");
		Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
		//删除数据
		sp.edit().remove("name").commit();
		sp.edit().clear().commit();
		
		}
	//存储到SharedPreferences
	public void save2sp2(View view) throws Exception{
        // /data/data/包名/shared_prefs/c.xml
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        //保存数据
        sp.edit().putString("name", "小明").apply();
        //获取数据
        String name = sp.getString("name", "");
        Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
        //删除数据
        sp.edit().remove("name").apply();
    }

commit()与apply()方法的区别在于:

  1. apply没有返回值而commit返回boolean表明修改是否提交成功
  2. apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步的提交到硬件磁盘,因此,在多个并发的提交commit的时候,他们会等待正在处理的commit保存到磁盘后在操作,从而降低了效率。而apply只是原子的提交到内容,后面有调用apply的函数的将会直接覆盖前面的内存数据,这样从一定程度上提高了很多效率。
  3. apply方法不会提示任何失败的提示。
    由于在一个进程中,sharedPreference是单实例,一般不会出现并发冲突,如果对提交的结果不关心的话,建议使用apply,当然需要确保提交成功且有后续操作的话,还是需要用commit的。
    ##4.存储到SQLite中
//存储到SQLite中
	public void save2sqlite(View view){
		
		//data/data/包名/databases/t_user.db
		MySQLiteOpenHelper openHelper = new MySQLiteOpenHelper(this, "t_user.db", null, 1);
		SQLiteDatabase database = openHelper.getReadableDatabase();
		
		//添加
		ContentValues values = new ContentValues();
		values.put("name", "小明");
		database.insert("t_user", null, values );
		
		//查询
		Cursor cursor = database.query("t_user", new String[]{"name","id"}, "id>?",new String[]{"1","小明"},null, null, null);
		
		while(cursor.moveToNext()){
			
			int index = cursor.getColumnIndex("name");
			String name = cursor.getString(index);
			int id = cursor.getInt(1);
		}
		cursor.close();
		database.close();
	}
	
	class MySQLiteOpenHelper extends SQLiteOpenHelper{

		public MySQLiteOpenHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

			String sql = "create table t_user (id integer primary key,name varchar(20))";
			db.execSQL(sql );
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			
		}
	}

你可能感兴趣的:(Android数据存储)