数据的存储有多种方式,比如数据库存储、SharedPreferences存储、文件存储等;
1. 基本使用
文件存储
**/storage/emulated/0/ 某某文件夹 : 0代表的是设备内存,1代表的是内存卡,
直接在内部储存里找 /0/ 后面那个某某文件夹,就可以找到。
/storage/emulated/0/Android/data/com.lzz.test/cache/files**
(1)FileOutputStream out = context.openFileOutput(String filename,int mode); 以mode模式获得文件输出流
(2)out.write(byte[]b);
FileOutputStream out = null;
out = context.openFileOutput(filename, Context.MODE_***);
out.write(filecontent.getBytes("UTF-8"));
out.close();
注意:文件默认会存储到/data/data/package/files中;
文件读取
(1)FileInputStream in = context.openFileInput(String filename); 获得某个文件的文件流
(2)int length = in.read(byte[]);
//每次读取固定的字节,并将此字节输出到字节输出流中,当全部读取完毕后,将字节流中的内容一并输出
FileInputStream in = null;
ByteArrayOutputStream bout = null;
byte[]buf = new byte[1024];
bout = new ByteArrayOutputStream();
int length = 0;
in = context.openFileInput(filename); //获得输入流
while((length=in.read(buf))!=-1){
bout.write(buf,0,length);
}
byte[] content = bout.toByteArray();
filecontentEt.setText(new String(content,"UTF-8")); //设置文本框为读取的内容
in.close();
bout.close();
注意:默认会读取/data/data/package/files的文件;
2. 文件模式
1.Context.MODE_PRIVATE:私有覆盖模式
- rw- rw- —
只能被当前应用访问,并且如果写入,则覆盖;
2.Context.MODE_APPEND:私有追加模式
- rw- rw- —
只能被当前应用访问,并且如果写入,则追加;
3.Context,MODE_WORLD_READABLE:公有只读模式 - rw- rw- r–
可以被其他应用读取;
4.Context.MODE_WORLD_WRITEABLE:公有可写模式 - rw- rw- -w-
可以被其他应用写入,但不能读取;
*注意,如果希望其他使得文件模式叠加,则可以使用加号连接;
比如:Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE 表示其他应用读写;
3. 简单使用
/** /data/data/package/files */
public class MainActivity extends Activity {
private String filecontent = "This is content----.";
private String filename = "title.txt";
private Button saveButton,readButton;
private Context context = this;
private OnClickListener listener = new OnClickListener(){
@Override
public void onClick(View v) {
if(v==saveButton){
FileOutputStream out = null;
try {
out = context.openFileOutput(filename, Context.MODE_PRIVATE);
out.write(filecontent.getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
else if(v==readButton){
FileInputStream in = null;
ByteArrayOutputStream bout = null;
byte[]buf = new byte[1024];
bout = new ByteArrayOutputStream();
int length = 0;
try {
in = context.openFileInput(filename); //获得输入流
while((length=in.read(buf))!=-1){
bout.write(buf,0,length);
}
byte[] content = bout.toByteArray();
Log.i("lzz",new String(content,"UTF-8")); //设置文本框为读取的内容
} catch (Exception e) {
e.printStackTrace();
}
//filecontentEt.invalidate(); //刷新屏幕
try{
in.close();
bout.close();
}
catch(Exception e){}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
saveButton = (Button)this.findViewById(R.id.saveButton);
readButton = (Button)this.findViewById(R.id.readButton);
saveButton.setOnClickListener(listener);
readButton.setOnClickListener(listener);
}
}
保存在SD卡中
通常情况下多数应用程序都会将缓存的位置选择为 /sdcard/Android/data//cache 这个路径。
选择在这个位置有两点好处:
第一,这是存储在SD卡上的,因此即使缓存再多的数据也不会对手机的内置存储空间有任何影响只要SD卡空间足够就行。
第二,这个路径被Android系统认定为应用程序的缓存路径,当程序被卸载的时候,这里的数据也会一起被清除掉,这样就不会出现删除程序之后手机上还有很多残留数据的问题。
假如没有此目录时,可以用以下
//路径例如: /SD卡/Android/data/程序的包名/cache/uniqueName
private static File getDiskCacheDir(Context context, String uniqueName) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return new File(cachePath + File.separator + uniqueName);
}
设置权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
存储到sdcard核心代码:
File f = new File(Environment.getExternalStorageDirectory(),filename);
out = new FileOutputStream(f,true);
out.write(filecontent.getBytes("UTF-8"));
/storage/emulated/0/title.txt
读取sdcard核心代码:
File f = new File(Environment.getExternalStorageDirectory(),filename);
in = new FileInputStream(f);
while((length=in.read(buf))!=-1){
bout.write(buf,0,length);
}
byte[] content = bout.toByteArray();