文件存储是利用Java的I/O流来实现向AndroID硬件磁盘上进行读写的 *** 作。
AndroID存储概念内部存储 Internal Storage :不可更改的,随着应用的卸载被删除
内部存储的特点:
默认只能被创建它的应用访问到当这个应用卸载后,内部存储中的文件也被删除一旦内部存储空间耗尽,手机也就无法使用/data/data/<applicationID>/shard_prefs
/data/data/<applicationID>/databases
/data/data/<applicationID>/files
/data/data/<applicationID>/cache
这四个文件夹都是属于内部存储。
前两个文件是通过系统提供的类和方法来获得文件的内容。
后两个文件是通过 context.getCacheDir()
和 context.getfilesDir()
这两个方法来得到。
外部存储 External Storage :可更改的。分为公有目录和私有目录,
公有目录:通过 Environment.getExternalStoragePublicDirectory(int type)
方法来获得公有目录下对应类型的文件。
私有目录:随着应用的卸载被删除/mnt/sdcard/data/data/<applicationID>/files
/mnt/sdcard/data/data/<applicationID>/cache
比较重要的两个类: fileOutputStream
、 fileinputStream
。
代码示例:
fileActivity的java文件:
public class fileActivity extends AppCompatActivity { private EditText mEtname; private button mBtnSave,mBtnShow; private TextVIEw mTvContent; private final String mFinalname = "text.txt"; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_file); mEtname=findVIEwByID(R.ID.et_name); mBtnSave=findVIEwByID(R.ID.btn_save); mBtnShow=findVIEwByID(R.ID.btn_show); mTvContent=findVIEwByID(R.ID.tv_content); mBtnSave.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { save(mEtname.getText().toString().trim()); //trim()表示去除前后空格,没有也可以 } }); mBtnShow.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { mTvContent.setText(read()); } }); } //存储数据 private voID save(String content){ fileOutputStream fileOutputStream=null; try { //创建存储目标 fileOutputStream = openfileOutput(mFinalname,MODE_PRIVATE); //字节方式存储方法 fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printstacktrace(); } finally { //关闭流 if (fileOutputStream!=null) { try { fileOutputStream.close(); } catch (IOException e) { e.printstacktrace(); } } } } //读取数据 private String read(){ fileinputStream fileinputStream = null; try { //获取读取文件 fileinputStream = openfileinput(mFinalname); //设置一次读取字节数 byte[] buff = new byte[1024]; //获取StringBuilder,实现字符串拼接 StringBuilder sb = new StringBuilder(""); int len = 0; //循环读取 while ((len = fileinputStream.read(buff)) > 0){ sb.append(new String(buff,0,len)); } //返回读取数据 return sb.toString(); } catch (IOException e) { e.printstacktrace(); } finally { //关闭流 if (fileinputStream!=null) { try { fileinputStream.close(); } catch (IOException e) { e.printstacktrace(); } } } return null; }}
布局文件:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" androID:padding="15dp"> <EditText androID:ID="@+ID/et_name" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:hint="输入内容" /> <button androID:ID="@+ID/btn_save" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="保存" androID:layout_margintop="10dp" /> <button androID:ID="@+ID/btn_show" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="显示" androID:layout_margintop="10dp" /> <TextVIEw androID:ID="@+ID/tv_content" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margintop="10dp" /></linearLayout>
运行效果:
总结:
存储数据:
获取fileOutputStream对象调用write()方法调用close()方法读取数据:
获取fileinputStream对象调用read()方法调用close()方法file外部存储 总结以上是内存溢出为你收集整理的Android开发笔记(二十一)——数据存储之文件存储全部内容,希望文章能够帮你解决Android开发笔记(二十一)——数据存储之文件存储所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)