欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)之文件操作詳解

 更新時間:2017年07月18日 10:29:16   作者:青蛙小王子  
這篇文章主要介紹了Android開發(fā)之文件操作,結(jié)合實例形式分析了Android開發(fā)中文件操作的步驟及布局、功能等實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Android開發(fā)之文件操作。分享給大家供大家參考,具體如下:

目前,幾乎所有的設備都會涉及到文件的操作,例如什么電腦,手機等設備。Android的文件操作和電腦是比較類似的,既可以存儲在手機內(nèi)置的存儲器里也可以是sd卡。在這篇文章里主要介紹在手機內(nèi)置存儲器里的文件操作。

一. 開發(fā)流程

(1)界面的設計
(2)設計android的業(yè)務層
(3)單元測試
(4)設置android的控制器層

二. 開發(fā)步驟

(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)設計業(yè)務層

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) { //通過構(gòu)造方法傳入context
    this.context = context;
  }
  //保存文件
  public void saveFile(String filename,String content) throws Exception{ //異常交給調(diào)用處處理
    FileOutputStream out=context.openFileOutput(filename, Context.MODE_PRIVATE);
    out.write(content.getBytes());
    out.close();
  }
  public String readFile(String filename) throws Exception{ //異常交給調(diào)用處處理
    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(); //把內(nèi)存里的數(shù)據(jù)讀取出來
    in.close(); //每個流都必須關(guān)閉
    array.close();
    return new String(data); //把byte數(shù)組轉(zhuǎn)換為字符串并返回
  }
}

下面開始做單元測試,要添加的環(huán)境就不說了

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">輸入文件內(nèi)容</string>
  <string name="button">保存</string>
  <string name="success">文件保存成功</string>
  <string name="failure">文件保存失敗</string>
</resources>

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設計有所幫助。

相關(guān)文章

最新評論