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

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

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

 Android 文件操作詳解

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

先從一個(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();
    }


  }


}

是不是感覺(jué)里面的代碼有點(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)用訪問(wèn)此文件,并且熟覆蓋式的添加數(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;
    }

  }

  /**
   * 獲得之前保存過(guò)的文件的詳細(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)樾蛄谢膯?wèn)題而出現(xiàn)不了目標(biāo)數(shù)據(jù)。
  3. 使用了返回值來(lái)對(duì)操作的結(jié)果進(jìn)行了“反饋”,方便為用戶提供友好的界面和使用體驗(yàn)。

核心

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

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

相關(guān)文章

  • Android中使用findViewByMe提升組件查找效率

    Android中使用findViewByMe提升組件查找效率

    本文主要介紹了Android中使用findViewByMe提升組件查找效率的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • Android檢測(cè)Cursor泄漏的原理以及使用方法

    Android檢測(cè)Cursor泄漏的原理以及使用方法

    本文介紹如何在 Android 檢測(cè) Cursor 泄漏的原理以及使用方法,還指出幾種常見(jiàn)的出錯(cuò)示例,同時(shí)該方法同樣適合于其他需要檢測(cè)資源泄露的情況,感興趣的朋友可以了解下
    2013-01-01
  • Android自動(dòng)測(cè)試工具M(jìn)onkey

    Android自動(dòng)測(cè)試工具M(jìn)onkey

    Monkey是Android中的一個(gè)命令行工具,可以運(yùn)行在模擬器里或?qū)嶋H設(shè)備中。它向系統(tǒng)發(fā)送偽隨機(jī)的用戶事件流(如按鍵輸入、觸摸屏輸入、手勢(shì)輸入等),實(shí)現(xiàn)對(duì)正在開發(fā)的應(yīng)用程序進(jìn)行壓力測(cè)試。Monkey測(cè)試是一種為了測(cè)試軟件的穩(wěn)定性、健壯性的快速有效的方法
    2016-01-01
  • Listview中Button搶占焦點(diǎn)的解決方法

    Listview中Button搶占焦點(diǎn)的解決方法

    在程序開發(fā)中經(jīng)常見(jiàn)到listview button搶占焦點(diǎn)的問(wèn)題,怎么回事什么原因呢?下面小編給大家?guī)?lái)了Listview中Button搶占焦點(diǎn)的解決方法,感興趣的朋友一起看下吧
    2016-08-08
  • android實(shí)現(xiàn)添加耳機(jī)狀態(tài)圖標(biāo)的方法

    android實(shí)現(xiàn)添加耳機(jī)狀態(tài)圖標(biāo)的方法

    這篇文章主要介紹了android實(shí)現(xiàn)添加耳機(jī)狀態(tài)圖標(biāo)的方法,較為詳細(xì)的分析了Android實(shí)現(xiàn)添加耳機(jī)圖標(biāo)的原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android仿貼吧內(nèi)容下的簡(jiǎn)單ListView嵌套GridView

    Android仿貼吧內(nèi)容下的簡(jiǎn)單ListView嵌套GridView

    這篇文章主要為大家詳細(xì)介紹了Android仿貼吧內(nèi)容下的簡(jiǎn)單ListView嵌套GridView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • android 有阻尼下拉刷新列表的實(shí)現(xiàn)方法

    android 有阻尼下拉刷新列表的實(shí)現(xiàn)方法

    下面小編就為大家分享一篇android 有阻尼下拉刷新列表的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Android編程中TextView字體屬性設(shè)置方法(大小、字體、下劃線、背景色)

    Android編程中TextView字體屬性設(shè)置方法(大小、字體、下劃線、背景色)

    這篇文章主要介紹了Android編程中TextView字體屬性設(shè)置方法,包括大小、字體、下劃線、背景色等設(shè)置技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android使用Javamail發(fā)送Email群發(fā)加附件

    Android使用Javamail發(fā)送Email群發(fā)加附件

    這篇文章主要為大家詳細(xì)介紹了Android使用Javamail發(fā)送Email群發(fā)加附件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Android筆記設(shè)計(jì)范例之日記APP實(shí)現(xiàn)全流程

    Android筆記設(shè)計(jì)范例之日記APP實(shí)現(xiàn)全流程

    這篇文章主要介紹了Android筆記設(shè)計(jì)范例之日記APP實(shí)現(xiàn)全流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-01-01

最新評(píng)論