最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 文件操作詳解及簡單實例

 更新時間:2017年02月19日 09:49:54   投稿:lqh  
這篇文章主要介紹了 Android 文件操作詳解及簡單實例的相關(guān)資料,需要的朋友可以參考下

 Android 文件操作詳解

Android 的文件操作說白了就是Java的文件操作的處理。所以如果對Java的io文件操作比較熟悉的話,android的文件操作就是小菜一碟了。好了,話不多說,開始今天的正題吧。

先從一個小項目入門吧

首先是一個布局文件,這一點比較的簡單,那就直接上代碼吧。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="文件名稱" />
  <EditText 
    android:id="@+id/et_filename"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="file name"
    />
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="文件內(nèi)容" />
  <EditText 
    android:id="@+id/et_filecontent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:lines="7"
    android:hint="file content"
    />
  <Button 
    android:id="@+id/btn_save"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="toSave"
    android:text="Save"
    />
  <Button 
    android:id="@+id/btn_get"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="getFile"
    android:text="Get"
    />


</LinearLayout>


然后是我們的主界面的Java文件了。繼續(xù)上代碼

package com.mark.storage;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mark.service.FileService;


public class MainActivity extends Activity {

  private EditText mEt_filename,mEt_filecontent;
  private Button mBtn_save;

  private void init(){
    mEt_filecontent = (EditText) findViewById(R.id.et_filecontent);
    mEt_filename = (EditText) findViewById(R.id.et_filename);
    mBtn_save = (Button) findViewById(R.id.btn_save);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
  }

  /**
   * 保存數(shù)據(jù)到一個文件中
   * @param view
   */
  public void toSave(View view) {
    String fileName = mEt_filename.getText().toString();
    String fileContent = mEt_filecontent.getText().toString();
    FileService service = new FileService(getApplicationContext());
    boolean isSucceed = service.save(fileName, fileContent);
    if(isSucceed){
      Toast.makeText(getApplicationContext(), "恭喜您保存文件成功!", Toast.LENGTH_SHORT).show();
    }else{
      Toast.makeText(getApplicationContext(), "對不起,您保存文件失?。?, Toast.LENGTH_SHORT).show();
    }
  }

  public void getFile(View view){
    String fileName = mEt_filename.getText().toString();

    FileService service = new FileService(getApplicationContext());
    String fileContent = service.getFile(fileName);
    if(fileContent!=null || !fileContent.equals("")) {
      mEt_filecontent.setText(fileContent);
    }else{
      Toast.makeText(getApplicationContext(), "對不起,讀取文件失??!", Toast.LENGTH_SHORT).show();
    }


  }


}

是不是感覺里面的代碼有點奇怪呢?FileService是什么鬼?

其實FileService就是我們的業(yè)務(wù)類,主要的功能就是幫助我們實現(xiàn)了對文件的保存和讀取等操作。下面也貼出代碼

package com.mark.service;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.content.Context;

public class FileService {

  //android自帶的可以快速獲得文件輸出流的一個類,注意參數(shù)不能是路徑,只能是文件名稱
  private Context mContext;

  public FileService(Context context) {
    this.mContext = context;
  }

  /**
   * 保存文件的一個方法
   * @param fileName
   * @param fileContent
   * @return
   */
  public boolean save(String fileName, String fileContent) {
    try {
      //采用Context.MODE_PRIVATE模式的話,只允許本應(yīng)用訪問此文件,并且熟覆蓋式的添加數(shù)據(jù)
      FileOutputStream fos = mContext.openFileOutput(fileName, Context.MODE_PRIVATE);
      fos.write(fileContent.getBytes());
      fos.close();
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }

  }

  /**
   * 獲得之前保存過的文件的詳細(xì)的信息
   * @param fileName
   * @return
   */
  public String getFile(String fileName) {
    String fileContent = "";
    try{

      FileInputStream fis = mContext.openFileInput(fileName);
      byte[] buf = new byte[1024];
      int len;
      ByteArrayOutputStream bais = new ByteArrayOutputStream();
      while((len = fis.read(buf))!= -1){
        bais.write(buf, 0, len);
      }
      byte[] data = bais.toByteArray();
      fileContent = new String(data);
      fis.close();
      return fileContent;
    }catch(Exception e){
      e.printStackTrace();
      return "對不起,讀取文件失?。?;
    }

  }


}

業(yè)務(wù)類的分析

現(xiàn)在開始進(jìn)入正題咯。這個小項目的核心就在于這個業(yè)務(wù)類,原因如下:

  1. Context:Android自帶的上下文類,方便獲得file流對象
  2. 讀文件方法中使用到了ByteArrayOutputStream類,這一點是很重要的,如果只是單純的使用字符串來讀取存儲的文件的話,就會因為序列化的問題而出現(xiàn)不了目標(biāo)數(shù)據(jù)。
  3. 使用了返回值來對操作的結(jié)果進(jìn)行了“反饋”,方便為用戶提供友好的界面和使用體驗。

核心

分層的思想,不同的功能的類放置到不同的包內(nèi),這樣既方便程序的調(diào)試,也方便今后的代碼的維護(hù)。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論

玉环县| 平凉市| 凤冈县| 石泉县| 冕宁县| 新营市| 仁化县| 石泉县| 辽阳县| 沐川县| 乌恰县| 保定市| 伊川县| 安义县| 宜都市| 定西市| 延津县| 马鞍山市| 镇坪县| 洞口县| 郓城县| 台前县| 德庆县| 宾川县| 钦州市| 昌乐县| 本溪市| 正阳县| 鞍山市| 措勤县| 武穴市| 余庆县| 泸州市| 苍溪县| 阆中市| 华蓥市| 汉中市| 乌拉特后旗| 阳城县| 郸城县| 平原县|