Android開發(fā)之文件操作詳解
本文實例講述了Android開發(fā)之文件操作。分享給大家供大家參考,具體如下:
目前,幾乎所有的設(shè)備都會涉及到文件的操作,例如什么電腦,手機等設(shè)備。Android的文件操作和電腦是比較類似的,既可以存儲在手機內(nèi)置的存儲器里也可以是sd卡。在這篇文章里主要介紹在手機內(nèi)置存儲器里的文件操作。
一. 開發(fā)流程
(1)界面的設(shè)計
(2)設(shè)計android的業(yè)務層
(3)單元測試
(4)設(shè)置android的控制器層
二. 開發(fā)步驟
(1)設(shè)計軟件界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/filename" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/content" android:minLines="3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button"/> </LinearLayout>
這里也把R文件給大家看看
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package org.lxh.file;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int button=0x7f050002;
public static final int content=0x7f050001;
public static final int filename=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int button=0x7f040004;
public static final int content=0x7f040003;
public static final int failure=0x7f040006;
public static final int filename=0x7f040002;
public static final int hello=0x7f040000;
public static final int success=0x7f040005;
}
}
(2)設(shè)計業(yè)務層
package org.lxh.service;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
import android.util.Log;
public class FileService {
private Context context;
public FileService(Context context) { //通過構(gòu)造方法傳入context
this.context = context;
}
//保存文件
public void saveFile(String filename,String content) throws Exception{ //異常交給調(diào)用處處理
FileOutputStream out=context.openFileOutput(filename, Context.MODE_PRIVATE);
out.write(content.getBytes());
out.close();
}
public String readFile(String filename) throws Exception{ //異常交給調(diào)用處處理
FileInputStream in=context.openFileInput(filename);
byte b[]=new byte[1024];
int len=0;
ByteArrayOutputStream array=new ByteArrayOutputStream();
while((len=in.read(b))!=-1){ //開始讀取文件
array.write(b,0,len);
}
byte data[]=array.toByteArray(); //把內(nèi)存里的數(shù)據(jù)讀取出來
in.close(); //每個流都必須關(guān)閉
array.close();
return new String(data); //把byte數(shù)組轉(zhuǎn)換為字符串并返回
}
}
下面開始做單元測試,要添加的環(huán)境就不說了
package org.lxh.test;
import org.lxh.service.FileService;
import android.test.AndroidTestCase;
import android.util.Log;
public class Test extends AndroidTestCase {
public static final String TAG = "Test";
public void testSave() {
FileService service = new FileService(this.getContext());
try {
service.saveFile("01.txt", "hello");
} catch (Exception e) {
Log.i(TAG, e.getMessage());
}
}
public void testRead() {
FileService service = new FileService(this.getContext());
try {
Log.i(TAG,service.readFile("01.txt"));
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}
看一下運行之后的效果


單元測試通過了,下面來看下在模擬器上的效果,在這之前要先看下下面的代碼
package org.lxh.file;
import org.lxh.service.FileService;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FileActivity extends Activity {
private FileService service;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service=new FileService(this);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText filename=(EditText)findViewById(R.id.filename);
EditText content=(EditText)findViewById(R.id.content);
try {
service.saveFile(filename.getText().toString(), content.getText().toString());
Toast.makeText(FileActivity.this, R.string.success, 1).show();
} catch (Exception e) {
Toast.makeText(FileActivity.this, R.string.failure, 1).show();
Log.e("FileActivity", e.getMessage());
}
}
});
}
}
如果保存成功就給用戶一個圖示通知:

下面把strings.xml的代碼也貼出來
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, FileActivity!</string> <string name="app_name">文件的讀取</string> <string name="filename">輸入文件名稱</string> <string name="content">輸入文件內(nèi)容</string> <string name="button">保存</string> <string name="success">文件保存成功</string> <string name="failure">文件保存失敗</string> </resources>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android 文件操作詳解及簡單實例
- android文件操作——讀取assets和raw文件下的內(nèi)容
- Android SD卡上文件操作及記錄日志操作實例分析
- Android中掃描多媒體文件操作詳解
- Android對sdcard擴展卡文件操作實例詳解
- Android 文件操作方法
- Android開發(fā)之文件操作模式深入理解
- Android中文件讀寫(輸入流和輸出流)操作小結(jié)
- Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法
- Android中使用pull解析器操作xml文件的解決辦法
- Android編程之在SD卡上進行文件讀寫操作實例詳解
- Android編程之文件讀寫操作與技巧總結(jié)【經(jīng)典收藏】
相關(guān)文章
Android內(nèi)嵌Unity并實現(xiàn)互相跳轉(zhuǎn)的實例代碼
這篇文章主要介紹了Android內(nèi)嵌Unity并實現(xiàn)互相跳轉(zhuǎn)的實例代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
AndroidStudio 3.6 中 R.layout 找不到對應的xml文件問題及解決方法
這篇文章主要介紹了AndroidStudio 3.6 中 R.layout 找不到對應的xml文件問題,本文給出了解決方法對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Android帶清除功能的輸入框控件EditTextWithDel
這篇文章主要為大家詳細介紹了Android帶清除功能的輸入框控件EditTextWithDel,感興趣的小伙伴們可以參考一下2016-09-09
Android仿百度谷歌搜索自動提示框AutoCompleteTextView簡單應用示例
這篇文章主要介紹了Android仿百度谷歌搜索自動提示框AutoCompleteTextView簡單應用,結(jié)合實例形式分析了AutoCompleteTextView Widget使用步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android開發(fā)基礎(chǔ)簡化Toast調(diào)用方法詳解
這篇文章主要為大家介紹了Android開發(fā)基礎(chǔ)簡化Toast調(diào)用方法的相關(guān)資料,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Android 異步獲取網(wǎng)絡圖片并處理導致內(nèi)存溢出問題解決方法
Android異步獲取網(wǎng)絡圖片并處理圖片Out Of Memory內(nèi)存溢出如何解決呢?本文介紹了操作步驟,感興趣的朋友可以了解下或許對你有所幫助2013-02-02

