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

