当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 文件操作详解及简单实例

Android 文件操作详解及简单实例

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

 android 文件操作详解

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

先从一个小项目入门吧

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

<linearlayout 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:orientation="vertical" >

  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="文件名称" />
  <edittext 
    android:id="@+id/et_filename"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="file name"
    />
  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="文件内容" />
  <edittext 
    android:id="@+id/et_filecontent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:lines="7"
    android:hint="file content"
    />
  <button 
    android:id="@+id/btn_save"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onclick="tosave"
    android:text="save"
    />
  <button 
    android:id="@+id/btn_get"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onclick="getfile"
    android:text="get"
    />


</linearlayout>


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

package com.mark.storage;

import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
import android.widget.toast;

import com.mark.service.fileservice;


public class mainactivity extends activity {

  private edittext met_filename,met_filecontent;
  private button mbtn_save;

  private void init(){
    met_filecontent = (edittext) findviewbyid(r.id.et_filecontent);
    met_filename = (edittext) findviewbyid(r.id.et_filename);
    mbtn_save = (button) findviewbyid(r.id.btn_save);
  }

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    init();
  }

  /**
   * 保存数据到一个文件中
   * @param view
   */
  public void tosave(view view) {
    string filename = met_filename.gettext().tostring();
    string filecontent = met_filecontent.gettext().tostring();
    fileservice service = new fileservice(getapplicationcontext());
    boolean issucceed = service.save(filename, filecontent);
    if(issucceed){
      toast.maketext(getapplicationcontext(), "恭喜您保存文件成功!", toast.length_short).show();
    }else{
      toast.maketext(getapplicationcontext(), "对不起,您保存文件失败!", toast.length_short).show();
    }
  }

  public void getfile(view view){
    string filename = met_filename.gettext().tostring();

    fileservice service = new fileservice(getapplicationcontext());
    string filecontent = service.getfile(filename);
    if(filecontent!=null || !filecontent.equals("")) {
      met_filecontent.settext(filecontent);
    }else{
      toast.maketext(getapplicationcontext(), "对不起,读取文件失败!", toast.length_short).show();
    }


  }


}

是不是感觉里面的代码有点奇怪呢?fileservice是什么鬼?

其实fileservice就是我们的业务类,主要的功能就是帮助我们实现了对文件的保存和读取等操作。下面也贴出代码

package com.mark.service;

import java.io.bytearrayoutputstream;
import java.io.fileinputstream;
import java.io.fileoutputstream;

import android.content.context;

public class fileservice {

  //android自带的可以快速获得文件输出流的一个类,注意参数不能是路径,只能是文件名称
  private context mcontext;

  public fileservice(context context) {
    this.mcontext = context;
  }

  /**
   * 保存文件的一个方法
   * @param filename
   * @param filecontent
   * @return
   */
  public boolean save(string filename, string filecontent) {
    try {
      //采用context.mode_private模式的话,只允许本应用访问此文件,并且熟覆盖式的添加数据
      fileoutputstream fos = mcontext.openfileoutput(filename, context.mode_private);
      fos.write(filecontent.getbytes());
      fos.close();
      return true;
    } catch (exception e) {
      e.printstacktrace();
      return false;
    }

  }

  /**
   * 获得之前保存过的文件的详细的信息
   * @param filename
   * @return
   */
  public string getfile(string filename) {
    string filecontent = "";
    try{

      fileinputstream fis = mcontext.openfileinput(filename);
      byte[] buf = new byte[1024];
      int len;
      bytearrayoutputstream bais = new bytearrayoutputstream();
      while((len = fis.read(buf))!= -1){
        bais.write(buf, 0, len);
      }
      byte[] data = bais.tobytearray();
      filecontent = new string(data);
      fis.close();
      return filecontent;
    }catch(exception e){
      e.printstacktrace();
      return "对不起,读取文件失败!";
    }

  }


}

业务类的分析

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

  1. context:android自带的上下文类,方便获得file流对象
  2. 读文件方法中使用到了bytearrayoutputstream类,这一点是很重要的,如果只是单纯的使用字符串来读取存储的文件的话,就会因为序列化的问题而出现不了目标数据。
  3. 使用了返回值来对操作的结果进行了“反馈”,方便为用户提供友好的界面和使用体验。

核心

分层的思想,不同的功能的类放置到不同的包内,这样既方便程序的调试,也方便今后的代码的维护。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网