一、保存在应用的私有文件下
// 实际保存路径/data/data/com.example.datasaver/files/ — getFilesDir()
// 实际保存路径/data/data/com.example.datasaver/cache/ — getCacheDir()
File file = new File(getFilesDir() ,”info.txt”);
File file = new File(getCacheDir() ,”info.txt”);
二、保存在公有的SdCard中
实际保存路径/storage/sdcard/
File file = new File(Environment.getExternalStorageDirectory(), “info.txt”);
为应用添加权限
三、使用sharedPrefereces方法保存数据,保存在xml文件中
默认保存在 /data/data/包名/shared_prefs 目录下.
SharedPreferences sp = getSharedPreferences(“info”, Context.MODE_PRIVATE);
Editor ed = sp.edit();
//将user变量内容写入
ed.putString(“user”, user);
//将pwd变量内容写入
ed.putString(“pwd”, pwd);
//提交内容
ed.apply();
四、自定义格式文件保存,这里不做介绍。
Android 下的四种数据存储方式
最新推荐文章于 2024-03-25 07:46:43 发布