目前,機會搜有的設備都會涉及到文件的操作,例如什麼電腦,手機等設備。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數組轉換為字符串並返回
- }
- }