目前,机会搜有的设备都会涉及到文件的操作,例如什么电脑,手机等设备。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) {  //通过构造方法传入contextthis.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>

如果对上面写的有疑问可以给我留言。


更多相关文章

  1. Android(安卓)NDK Hello + JNI 与 NDK区别
  2. Android开发中使用矢量图
  3. Android分享文件到微信和QQ功能的实现(兼容android 7.0以上的共享
  4. Android热修复之AndFix
  5. 系统总结归纳一下android
  6. app在android studio的构建过程
  7. 【高明鑫】014 android之 资源管理详解
  8. Android(安卓)- 利用Android(安卓)studio + Android(安卓)Killer
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android输入法的显示与隐藏
  2. Android ImageView去掉周围的白边
  3. 关于一个android工程同时使用多个工程库,
  4. 从源码一次彻底理解Android的消息机制
  5. Android Google应用移植时包依赖关系
  6. Android积木之图片的生成和保存
  7. android上多样式文本的使用
  8. 给android设置代理
  9. [Android]CircleList 圆弧形 ListView
  10. android实现退出时关闭所有activity