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>
- /* 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;
- }
- }
- 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数组转换为字符串并返回
- }
- }
|
评论暂时关闭