Android 文件操作詳解及簡單實例
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ù)類,原因如下:
- Context:Android自帶的上下文類,方便獲得file流對象
- 讀文件方法中使用到了ByteArrayOutputStream類,這一點是很重要的,如果只是單純的使用字符串來讀取存儲的文件的話,就會因為序列化的問題而出現(xiàn)不了目標(biāo)數(shù)據(jù)。
- 使用了返回值來對操作的結(jié)果進(jìn)行了“反饋”,方便為用戶提供友好的界面和使用體驗。
核心
分層的思想,不同的功能的類放置到不同的包內(nèi),這樣既方便程序的調(diào)試,也方便今后的代碼的維護(hù)。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- android文件操作——讀取assets和raw文件下的內(nèi)容
- Android SD卡上文件操作及記錄日志操作實例分析
- Android中掃描多媒體文件操作詳解
- Android對sdcard擴(kuò)展卡文件操作實例詳解
- Android 文件操作方法
- Android開發(fā)之文件操作模式深入理解
- Android中文件讀寫(輸入流和輸出流)操作小結(jié)
- Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法
- Android中使用pull解析器操作xml文件的解決辦法
- Android編程之在SD卡上進(jìn)行文件讀寫操作實例詳解
- Android編程之文件讀寫操作與技巧總結(jié)【經(jīng)典收藏】
- Android開發(fā)之文件操作詳解
相關(guān)文章
android獲取圖片尺寸的兩種方式及bitmap的縮放操作
這篇文章主要介紹了android獲取圖片尺寸的兩種方式及bitmap的縮放操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法
今天小編就為大家分享一篇關(guān)于Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Android自定義view之利用drawArc方法實現(xiàn)動態(tài)效果(思路詳解)
這篇文章主要介紹了Android自定義view之利用drawArc方法實現(xiàn)動態(tài)效果,drawArc方法包含了五個參數(shù),具體細(xì)節(jié)在本文中給大家提到過,需要的朋友可以參考下2021-08-08
Android用MVP實現(xiàn)一個簡單的類淘寶訂單頁面的示例
本篇文章主要介紹了Android用MVP實現(xiàn)一個簡單的類淘寶訂單頁面的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
詳解Activity之singletast啟動模式及如何使用intent傳值
在一個新棧中創(chuàng)建該Activity實例,并讓多個應(yīng)用共享改棧中的該Activity實例。一旦改模式的Activity的實例存在于某個棧中,任何應(yīng)用再激活改Activity時都會重用該棧中的實例,其效果相當(dāng)于多個應(yīng)用程序共享一個應(yīng)用,不管誰激活該Activity都會進(jìn)入同一個應(yīng)用中2015-11-11
Android仿Iphone屏幕底部彈出半透明PopupWindow效果
這篇文章主要為大家詳細(xì)介紹了Android仿Iphone屏幕底部彈出半透明PopupWindow效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Android自定義View實現(xiàn)圓弧進(jìn)度的效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)圓弧進(jìn)度的效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-01-01
Android 中使用RecyclerView實現(xiàn)底部翻頁
這篇文章主要介紹了Android 中使用RecyclerView實現(xiàn)底部翻頁功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11
android之計時器(Chronometer)的使用以及常用的方法
在Android的SDK中,為我們提供了一個計時器,這個計時器稱為Chronometer,我們可以成它為Android的一個組件,同時它也具備自己獨有的方法2013-01-01

