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

Android 文件操作詳解及簡(jiǎn)單實(shí)例

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

 Android 文件操作詳解

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

先從一個(gè)小項(xiàng)目入門吧

首先是一個(gè)布局文件,這一點(diǎn)比較的簡(jiǎn)單,那就直接上代碼吧。

<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ù)到一個(gè)文件中
   * @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(), "對(duì)不起,您保存文件失?。?, 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(), "對(duì)不起,讀取文件失敗!", Toast.LENGTH_SHORT).show();
    }


  }


}

是不是感覺里面的代碼有點(diǎn)奇怪呢?FileService是什么鬼?

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

package com.mark.service;

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

import android.content.Context;

public class FileService {

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

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

  /**
   * 保存文件的一個(gè)方法
   * @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 "對(duì)不起,讀取文件失敗!";
    }

  }


}

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

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

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

核心

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

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

相關(guān)文章

最新評(píng)論

郓城县| 兴安县| 东阳市| 界首市| 兴国县| 集贤县| 阆中市| 通道| 德州市| 西乌珠穆沁旗| 白沙| 本溪市| 云浮市| 甘谷县| 常熟市| 纳雍县| 台南县| 长宁县| 镇平县| 东台市| 寿宁县| 和林格尔县| 台东市| 营山县| 望奎县| 玛沁县| 安阳市| 香港 | 丘北县| 嵩明县| 章丘市| 桐梓县| 苏州市| 晋宁县| 五寨县| 新郑市| 普宁市| 红原县| 集安市| 建德市| 抚远县|