当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发之文件操作详解

Android开发之文件操作详解

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

青银高速大货起火,古诗江南春,网上交违章罚款

本文实例讲述了android开发之文件操作。分享给大家供大家参考,具体如下:

目前,几乎所有的设备都会涉及到文件的操作,例如什么电脑,手机等设备。android的文件操作和电脑是比较类似的,既可以存储在手机内置的存储器里也可以是sd卡。在这篇文章里主要介绍在手机内置存储器里的文件操作。

一. 开发流程

(1)界面的设计
(2)设计android的业务层
(3)单元测试
(4)设置android的控制器层

二. 开发步骤

(1)设计软件界面

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<textview
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/filename"
  />
 <edittext
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/filename"
  />
  <textview
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/content"
  />
 <edittext
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/content"
  android:minlines="3"
  />
  <button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/button"
   android:id="@+id/button"/>
</linearlayout>

这里也把r文件给大家看看

/* auto-generated file. do not modify.
 *
 * this class was automatically generated by the
 * aapt tool from the resource data it found. it
 * should not be modified by hand.
 */
package org.lxh.file;
public final class r {
  public static final class attr {
  }
  public static final class drawable {
    public static final int icon=0x7f020000;
  }
  public static final class id {
    public static final int button=0x7f050002;
    public static final int content=0x7f050001;
    public static final int filename=0x7f050000;
  }
  public static final class layout {
    public static final int main=0x7f030000;
  }
  public static final class string {
    public static final int app_name=0x7f040001;
    public static final int button=0x7f040004;
    public static final int content=0x7f040003;
    public static final int failure=0x7f040006;
    public static final int filename=0x7f040002;
    public static final int hello=0x7f040000;
    public static final int success=0x7f040005;
  }
}

(2)设计业务层

package org.lxh.service;
import java.io.bytearrayoutputstream;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import android.content.context;
import android.util.log;
public class fileservice {
  private context context;
  public fileservice(context context) { //通过构造方法传入context
    this.context = context;
  }
  //保存文件
  public void savefile(string filename,string content) throws exception{ //异常交给调用处处理
    fileoutputstream out=context.openfileoutput(filename, context.mode_private);
    out.write(content.getbytes());
    out.close();
  }
  public string readfile(string filename) throws exception{ //异常交给调用处处理
    fileinputstream in=context.openfileinput(filename);
    byte b[]=new byte[1024];
    int len=0;
    bytearrayoutputstream array=new bytearrayoutputstream();
    while((len=in.read(b))!=-1){ //开始读取文件
      array.write(b,0,len);
    }
    byte data[]=array.tobytearray(); //把内存里的数据读取出来
    in.close(); //每个流都必须关闭
    array.close();
    return new string(data); //把byte数组转换为字符串并返回
  }
}

下面开始做单元测试,要添加的环境就不说了

package org.lxh.test;
import org.lxh.service.fileservice;
import android.test.androidtestcase;
import android.util.log;
public class test extends androidtestcase {
  public static final string tag = "test";
  public void testsave() {
    fileservice service = new fileservice(this.getcontext());
    try {
      service.savefile("01.txt", "hello");
    } catch (exception e) {
      log.i(tag, e.getmessage());
    }
  }
  public void testread() {
    fileservice service = new fileservice(this.getcontext());
    try {
      log.i(tag,service.readfile("01.txt"));
    } catch (exception e) {
      log.e(tag, e.getmessage());
    }
  }
}

看一下运行之后的效果

单元测试通过了,下面来看下在模拟器上的效果,在这之前要先看下下面的代码

package org.lxh.file;
import org.lxh.service.fileservice;
import android.app.activity;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
import android.widget.toast;
public class fileactivity extends activity {
  private fileservice service;
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    service=new fileservice(this);
    button button=(button)findviewbyid(r.id.button);
    button.setonclicklistener(new view.onclicklistener() {
      public void onclick(view v) {
        edittext filename=(edittext)findviewbyid(r.id.filename);
        edittext content=(edittext)findviewbyid(r.id.content);
        try {
          service.savefile(filename.gettext().tostring(), content.gettext().tostring());
          toast.maketext(fileactivity.this, r.string.success, 1).show();
        } catch (exception e) {
          toast.maketext(fileactivity.this, r.string.failure, 1).show();
          log.e("fileactivity", e.getmessage());
        }
      }
    });
  }
}

如果保存成功就给用户一个图示通知:

下面把strings.xml的代码也贴出来

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">hello world, fileactivity!</string>
  <string name="app_name">文件的读取</string>
  <string name="filename">输入文件名称</string>
  <string name="content">输入文件内容</string>
  <string name="button">保存</string>
  <string name="success">文件保存成功</string>
  <string name="failure">文件保存失败</string>
</resources>

更多关于android相关内容感兴趣的读者可查看本站专题:《android文件操作技巧汇总》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android布局layout技巧总结》、《android开发入门与进阶教程》、《android资源操作技巧汇总》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

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

相关文章:

验证码:
移动技术网