当前位置: 移动技术网 > IT编程>移动开发>Android > Android 数据存储方式有哪几种

Android 数据存储方式有哪几种

2019年07月24日  | 移动技术网IT编程  | 我要评论

超级中国第四集,kmplayer设置,雷迪嘎嘎的师弟

以下内容给大家介绍android数据存储提供了五种方式:

1、sharedpreferences

2、文件存储

3、sqlite数据库

4、contentprovider

5、网络存储

  本文主要介绍如何使用文件来存储数据。android文件操作用到的是java.io中的fileoutputstream和fileinputstream类。

一、存储文件

  首先实例化一个fileoutputstream。

fileoutputstream fostream = openfileoutput(filename, mode_private);
// filename: 要写入文件的名称
// mode_private: 为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
// mode_append: 模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
// mode_world_readable: 表示当前文件可以被其他应用读取,不推荐使用
// mode_world_writeable: 表示当前文件可以被其他应用写入,不推荐使用

  然后调用fostream.write()即可完成写入。

byte[] buffer = filecontent.getbytes();
fostream.write(buffer);
toast.maketext(mainactivity.this, "写入成功",toast.length_short).show();

  最后进行一些清理工作,刷新写出流和关闭流。

fostream.flush();
fostream.close();

二、读取文件

  同样的,首先实例化一个fileinputstream。

fileinputstream fistream = openfileinput(filename)

  然后调用fistream.read()即可

int len = fistream.available();
byte[] buffer = new byte[len];
fistream.read(buffer)

  最后,将文本显示并关闭读文件流

etcontent.settext(new string(buffer));
toast.maketext(mainactivity.this, "读取成功",toast.length_short).show();
fistream.close();

三、完整代码

import android.support.v.app.appcompatactivity;
 import android.os.bundle;
 import android.view.view;
 import android.widget.button;
 import android.widget.edittext;
 import android.widget.toast;
 import java.io.fileinputstream;
 import java.io.fileoutputstream;
 public class mainactivity extends appcompatactivity {
   private edittext etname;
   private edittext etcontent;
   private button btnwrite;
   private button btnread;
   private string filename = "";
   private string filecontent = "";
   @override
   protected void oncreate(bundle savedinstancestate) {
     super.oncreate(savedinstancestate);
     setcontentview(r.layout.activity_main);
     etname = (edittext)findviewbyid(r.id.etname);
     etcontent = (edittext)findviewbyid(r.id.etcontent);
     btnwrite = (button)findviewbyid(r.id.btnwrite);
     btnread = (button)findviewbyid(r.id.btnread);
     btnwrite.setonclicklistener(new view.onclicklistener() {
       @override
       public void onclick(view view) {
         filename = etname.gettext().tostring();
         filecontent = etcontent.gettext().tostring();
         try {
           fileoutputstream fostream = openfileoutput(filename, mode_private);
           byte[] buffer = filecontent.getbytes();
           fostream.write(buffer);
           toast.maketext(mainactivity.this, "写入成功",toast.length_short).show();
           fostream.flush();
           fostream.close();
         }catch(exception e){
           e.printstacktrace();
         }
       }
     });
     btnread.setonclicklistener(new view.onclicklistener() {
       @override
       public void onclick(view view) {
         filename = etname.gettext().tostring();
         try{
           fileinputstream fistream = openfileinput(filename);
           int len = fistream.available();
           byte[] buffer = new byte[len];
           fistream.read(buffer);
           etcontent.settext(new string(buffer));
           toast.maketext(mainactivity.this, "读取成功",toast.length_short).show();
           fistream.close();
         }catch(exception e){
           e.printstacktrace();
         }
       }
     });
   }
 }
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin"
   android:paddingright="@dimen/activity_horizontal_margin"
   android:paddingtop="@dimen/activity_vertical_margin"
   android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity">
   <edittext
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/etname"
     android:layout_alignparenttop="true"
     android:layout_alignparentleft="true"
     android:layout_alignparentstart="true"
     android:layout_alignparentright="true"
     android:layout_alignparentend="true"
     android:text="文件名" />
   <edittext
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/etcontent"
     android:layout_below="@+id/etname"
     android:layout_alignparentleft="true"
     android:layout_alignparentstart="true"
     android:layout_alignparentright="true"
     android:layout_alignparentend="true"
     android:text="文件内容" />
   <button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="保存"
     android:id="@+id/btnwrite"
     android:layout_aligntop="@+id/btnread"
     android:layout_toleftof="@+id/btnread"
     android:layout_tostartof="@+id/btnread" />
   <button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="读取"
     android:id="@+id/btnread"
     android:layout_below="@+id/etcontent"
     android:layout_alignparentright="true"
     android:layout_alignparentend="true" />
 </relativelayout>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网