Android开发之文件操作详解

 更新时间:2017年07月18日 10:29:16   作者:青蛙小王子  
这篇文章主要介绍了Android开发之文件操作,结合实例形式分析了Android开发中文件操作的步骤及布局、功能等实现技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • Android 类似微信登录输入框效果

    Android 类似微信登录输入框效果

    这篇文章主要介绍了Android 类似微信登录输入框效果,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-05-05
  • Android录制按钮源码解析

    Android录制按钮源码解析

    这篇文章主要为大家详细解析了Android录制按钮源码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • Android开发学习实现简单计算器

    Android开发学习实现简单计算器

    这篇文章主要为大家详细介绍了Android实现一个简单计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • eclipse搭建android开发环境详细步骤

    eclipse搭建android开发环境详细步骤

    本文主要介绍了eclipse搭建android开发环境详细步骤,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • android之listview悬浮topBar效果

    android之listview悬浮topBar效果

    这篇文章主要为大家详细介绍了android之listview悬浮topBar效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Android Fragment与Activity之间的相互通信实例代码

    Android Fragment与Activity之间的相互通信实例代码

    这篇文章主要介绍了Android Fragment与Activity之间的相互通信的相关资料,并附简单实例代码,需要的朋友可以参考下
    2016-11-11
  • 导致adb无法启动的5种情况和解决方法

    导致adb无法启动的5种情况和解决方法

    这篇文章主要介绍了导致adb无法启动的5种情况和解决方法,本文列举了最常见的5种情况和对应解决方法,需要的朋友可以参考下
    2015-04-04
  • Android中的Bmob移动后端云服务器功能

    Android中的Bmob移动后端云服务器功能

    这里介绍一个移动后端云服务器平台bmob,这不仅可以实现云数据库储存,还可以获取手机验证等,随时随地都很轻松,下面写一个小demo,实现一个登陆注册功能,认识增删查改
    2018-01-01
  • Android实现横屏切换科学计算器

    Android实现横屏切换科学计算器

    这篇文章主要为大家详细介绍了Android实现横屏切换科学计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • Android webview加载https链接错误或无响应的解决

    Android webview加载https链接错误或无响应的解决

    这篇文章主要介绍了Android webview加载https链接错误或无响应的解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03

最新评论