Android开发中如何实现操作文件

  介绍

今天就跟大家聊聊有关Android开发中如何实现操作文件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

<强>,Android文件操作详解

Android的文件操作说白了就是Java的文件操作的处理,所以如果对Java的io文件操作比较熟悉的话,Android的文件操作就是小菜一碟了。好了,话不多说,开始今天的正题吧。

先从一个小项目入门吧

首先是一个布局文件,这一点比较的简单,那就直接上代码吧。

& lt; LinearLayout xmlns: Android=癶ttp://schemas.android.com/apk/res/android"   xmlns:工具=癶ttp://schemas.android.com/tools"   android: layout_width=癿atch_parent"   android: layout_height=癿atch_parent"   android:取向=皏ertical"比;      & lt; TextView   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android:文本=拔募啤?比;   EditText & lt;   android: id=癅 + id/et_filename"   android: layout_width=癿atch_parent"   android: layout_height=皐rap_content"   android:提示=拔募ame"/比;   & lt; TextView   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android:文本=拔募谌?/比;   EditText & lt;   android: id=癅 + id/et_filecontent"   android: layout_width=癿atch_parent"   android: layout_height=皐rap_content"   android:行=?”;   android:提示=拔募ontent"/比;   & lt;按钮   android: id=癅 + id/btn_save"   android: layout_width=癿atch_parent"   android: layout_height=皐rap_content"   android: onClick=皌oSave"   android:文本=癝ave"/比;   & lt;按钮   android: id=癅 + id/btn_get"   android: layout_width=癿atch_parent"   android: layout_height=皐rap_content"   android: onClick=癵etFile"   android:文本=癎et"/比;         & lt;/LinearLayout>

然后是我们的主界面的Java文件了。继续上代码

包com.mark.storage;
  
  进口android.app.Activity;
  进口android.os.Bundle;
  进口android.view.View;
  进口android.widget.Button;
  进口android.widget.EditText;
  进口android.widget.Toast;
  
  进口com.mark.service.FileService;
  
  
  公开课MainActivity延伸活动{
  
  私人EditText mEt_filename mEt_filecontent;
  私人按钮mBtn_save;
  
  私人空间init () {
  EditText mEt_filecontent=() findViewById (R.id.et_filecontent);
  EditText mEt_filename=() findViewById (R.id.et_filename);
  mBtn_save=(按钮)findViewById (R.id.btn_save);
  }
  
  @Override
  保护空白>包com.mark.service;
  
  进口java.io.ByteArrayOutputStream;
  进口java.io.FileInputStream;
  进口java.io.FileOutputStream;
  
  进口android.content.Context;
  
  公开课FileService {//android自带的可以快速获得文件输出流的一个类,注意参数不能是路径,只能是文件名称
  私人上下文mContext;
  
  公共FileService(上下文语境){
  这一点。mContext=上下文;
  }/* *
  *保存文件的一个方法
  * @param文件名
  * @param fileContent
  * @return
  */公共布尔省(文件名的字符串,字符串fileContent) {
  尝试{//采用Context.MODE_PRIVATE模式的话,只允许本应用访问此文件,并且熟覆盖式的添加数据
  FileOutputStream安全系数=mContext。openFileOutput(文件名,Context.MODE_PRIVATE);
  fos.write (fileContent.getBytes ());
  fos.close ();
  返回true;
  }捕捉(异常e) {
  e.printStackTrace ();
  返回错误;
  }
  
  }/* *
  *获得之前保存过的文件的详细的信息
  * @param文件名
  * @return
  */公共字符串getFile(字符串文件名){
  字符串fileContent=?“;
  尝试{
  
  FileInputStream fis=mContext.openFileInput(文件名);
  byte [] buf=新字节[1024];
  int len;
  ByteArrayOutputStream姨=new ByteArrayOutputStream ();
  而((len=fis.read (buf)) !=1) {
  拜斯市。写(buf 0 len);
  }
  byte [] data=https://www.yisu.com/zixun/bais.toByteArray ();
  fileContent=新的字符串(数据);
  fis.close ();
  返回fileContent;
  }捕捉(异常e) {
  e.printStackTrace ();
  回归”对不起,读取文件失败!”;
  }
  
  }
  
  
  }

<>强业务类的分析

现在开始进入正题咯。这个小项目的核心就在于这个业务类,原因如下:

    <李>背景:Android自带的上下文类,方便获得文件流对象李

    Android开发中如何实现操作文件