SharedPreferences
写数据
SharedPreferences preferences = getSharedPreferences("songdingxing", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("username","送定型");
editor.putInt("age",18);
editor.putBoolean("isMan",false);
editor.putFloat("price",12.4f);
editor.putLong("id",5425054250l);
editor.commit();
读数据
SharedPreferences preferences = getSharedPreferences("sgf", MODE_PRIVATE);
String username=preferences.getString("username","");
int age=preferences.getInt("age",0);
boolean isMan=preferences.getBoolean("isMan",false);
float price=preferences.getFloat("price",0.0f);
long id=preferences.getLong("id",0l);
Toast.makeText(this, username+":"+age+":"+isMan+":"+price+":"+id, Toast.LENGTH_SHORT).show();
SD卡储存
SD卡介绍:
一般手机文件管理 根路径 /storage/emulated/0/或者/mnt/shell/emulated/0
写数据
//获得运行时的权限
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},100);
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取SD卡的路径
File file = Environment.getExternalStorageDirectory();
FileOutputStream out=null;
try {
out = new FileOutputStream(new File(file, "aaa.txt"));
out.write("哈哈哈哈".getBytes());
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
读数据
//获得运行时的权限
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},100); if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file = Environment.getExternalStorageDirectory();
FileInputStream inputStream=null;
StringBuffer sb =new StringBuffer();
try {
inputStream = new FileInputStream(new File(file, "aaa.txt"));
byte[] bytes = new byte[1024];
int len=0;
while ((len=inputStream.read(bytes))!=-1){
sb.append(new String(bytes,0,len));
}
showId.setText(sb.toString());
Log.i(TAG, "onClick: "+sb.toString());
} catch (Exception e) {
e.printStackTrace();
}finally {
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}