当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

6.1 数据存储与访问之——文件存储读写

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:26:34

本节引言:嘿嘿,看到这个题目,相信部分读者会问,你前面的Fragment写完了吗?嗯,没写完,因为想例子,需要一点时间,为了提高效率,所以决定像多线程一样,并发的来写教程,这样可能可以加快写教程的进度,到现在为止,刚好写了60篇,离完成入门教程还很远呢,而前面也说过,想在一个半到两个月之内完成这套教程,今天已经9.1号了,要加吧劲~好的,废话就这么多,本节给大家介绍的是Android数据存储与访问方式中的一个——文件存储与读写,当然除了这种方式外,我们可以存到SharedPreference,数据库,或者Application中,当然这些后面都会讲,嗯,开始本节内容~1.Android文件的操作模式学过Java的同学都知道,我们新建文件,然后就可以写入数据了,但是Android却不一样,因为Android是基于Linux的,我们在读写文件的时候,还需加上文件的操作模式,Android中的操作模式如下:2.文件的相关操作方法3.文件读写的实现Android中的文件读写和Java中的文件I/O相同,流程也很简单,下面我们来写个简单的示例:实现效果图:PS:这里用的是模拟器,因为笔者的N5并没有root,看不到文件的存储目录,下面我们打开DDMS的File Exploer可以看到,在data/data/<包名>/file中有我们写入的文件:我们可以点击右上角的响应图标将文件导入到电脑中,并且打开验证写入的内容: 代码实现:首先是布局文件:main_activity.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jay.example.filedemo1.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nametitle" /> <EditText android:id="@+id/editname" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/detailtitle" /> <EditText android:id="@+id/editdetail" android:layout_width="match_parent" android:layout_height="wrap_content" android:minLines="2" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btnsave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnwrite" /> <Button android:id="@+id/btnclean" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnclean" /> </LinearLayout> <Button android:id="@+id/btnread" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnread" /></LinearLayout>然后我们来写一个文件协助类:FileHelper.java/** * Created by Jay on 2015/9/1 0001. */public class FileHelper { private Context mContext; public FileHelper() { } public FileHelper(Context mContext) { super(); this.mContext = mContext; } /* * 这里定义的是一个文件保存的方法,写入到文件中,所以是输出流 * */ public void save(String filename, String filecontent) throws Exception { //这里我们使用私有模式,创建出来的文件只能被本应用访问,还会覆盖原文件哦 FileOutputStream output = mContext.openFileOutput(filename, Context.MODE_PRIVATE); output.write(filecontent.getBytes()); //将String字符串以字节流的形式写入到输出流中 output.close(); //关闭输出流 } /* * 这里定义的是文件读取的方法 * */ public String read(String filename) throws IOException { //打开文件输入流 FileInputStream input = mContext.openFileInput(filename); byte[] temp = new byte[1024]; StringBuilder sb = new StringBuilder(""); int len = 0; //读取文件内容: while ((len = input.read(temp)) > 0) { sb.append(new String(temp, 0, len)); } //关闭输入流 input.close(); return sb.toString(); }}最后是MainActivity.java,我们在这里完成相关操作:public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText editname; private EditText editdetail; private Button btnsave; private Button btnclean; private Button btnread; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); bindViews(); } private void bindViews() { editdetail = (EditText) findViewById(R.id.editdetail); editname = (EditText) findViewById(R.id.editname); btnclean = (Button) findViewById(R.id.btnclean); btnsave = (Button) findViewById(R.id.btnsave); btnread = (Button) findViewById(R.id.btnread); btnclean.setOnClickListener(this); btnsave.setOnClickListener(this); btnread.setOnClickListener(this); } @Override public void onClick(View

[1] [2] [3]  下一页


6.1 数据存储与访问之——文件存储读写