四.文件的存储于访问
(1)文件
文件的保存在手机自带存储空间:
public class fileService {
private Context context;
public fileService(Context context) {
this.context = context;
}
public void save(String fileName, String fileContect)throws Exception {
//私有操作模式,创建出来的文件只能被被应用访问,写入文件中的内容会覆盖原文件。默认保存在包名下的file文件中
FileOutputStream fos = context.openFileOutput(fileName,Context.MODE_PRIVATE);
fos.write(fileContect.getBytes());
fos.close();
}
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText name = (EditText)findViewById(R.id.file_et);
EditText contect = (EditText)findViewById(R.id.file_contect);
String fileName = name.getText().toString();
String fileContect = contect.getText().toString();
fileService service = new fileService(getApplication());
try {
service.save(fileName,fileContect);
Toast.makeText(MainActivity.this,R.string.save_success,Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this,R.string.save_fail,Toast.LENGTH_LONG).show();
e.printStackTrace();
}
读取文件:
public String read(String fileName) throws Exception {
//私有操作模式,创建出来的文件只能被被应用访问,写入文件中的内容会覆盖原文件。默认保存在包名下的file文件中
FileInputStream fis = context.openFileInput(fileName);
ByteArrayOutputStream bas = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer))!= -1){
bas.write(buffer,0,len);
}
fis.close();
byte[] data = bas.toByteArray();
return new String(data);
}
介绍四种操作模式:
MODE_PRIVATE: 私有操作模式,创建出来的文件只能被被应用访问,写入文件中的内容会覆盖原文件。默认保存在包名下的file文件中
MODE_APPEAD: 检查文件是否存在,如果存在,就追加,否则就创建新文件,而且也是只能被本文件访问。
MODE_WORLD_WRITEABLE: 表示当前文件可以被其他应用写入
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取
Activity还提供了getCacheDir()和getFilesDir()方法:
getCacheDir()方法用于获取 /data/data/<package name> cache目录
getFilesDir() 方法用于获取 /data/data/<package name>/files 目录
将 文件保存在SD卡上:
<!--在SD卡中创建与删除文件的权限-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--往SD卡中写入文件的权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
// 文件写入SD卡
public void saveSDCard(String fileName,String contect) throws Exception{
// 指定文件的写入位置
File file = new File(new File("/mnt/sdcard"),fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(contect.getBytes());
fos.close();
}
要注意再调用方法时判断sd卡的状态:
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
service.saveSDCard(fileName,contect);
//提示写入成功
}
}catch (Exception e){
//提示写入失败
}
获取sd卡路径:
Environment.getExternalStorageDirectory()
(2)SharedPerference
存储数据:
public class PreferencesService {
private Context context;
public PreferencesService(Context context) {
this.context = context;
}
//保存参数
public void save(String name, Integer age) {
//用上下文对象的到spf
SharedPreferences spf = context.getSharedPreferences("weimiao",Context.MODE_PRIVATE);
//得到编辑器,用编辑器
SharedPreferences.Editor editor = spf.edit();
editor.putString("name",name);
editor.putInt("age",age);
//数据提交回文件中
editor.commit();
}
//将开始输入的数据下次打开时回显在屏幕上
public Map<String,String> getShareparences(){
Map<String,String> map = new HashMap<>();
SharedPreferences sp =context.getSharedPreferences("weimiao",Context.MODE_PRIVATE);
map.put("name",sp.getString("name",""));
map.put("age",String.valueOf(sp.getInt("age",0)));
return map;
}
}
测试:
public class MainActivity extends Activity {
private EditText nameText;
private EditText ageText;
private PreferencesService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameText =(EditText)findViewById(R.id.et_name);
ageText =(EditText)findViewById(R.id.et_age);
service = new PreferencesService(this);
//用来回显数据
Map<String,String> map = service.getShareparences();
nameText.setText(map.get("name"));
ageText.setText(map.get("age"));
}
public void save(View v){
String name = nameText.getText().toString();
String age = ageText.getText().toString();
service.save(name,Integer.valueOf(age));
Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
}