Android應(yīng)用程序中讀寫txt文本文件的基本方法講解

最終效果圖,點(diǎn)擊save會(huì)保存到文件中,點(diǎn)擊show會(huì)從文件中讀取出內(nèi)容并顯示。

main.xml
<?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="請(qǐng)您輸入要保存的內(nèi)容:" /> <EditText android:id="@+id/addText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="請(qǐng)您在此處輸入文件內(nèi)容!" /> <Button android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="save" /> <Button android:id="@+id/showButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="show" /> <TextView android:id="@+id/showText" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
activity代碼
package cn.com.file;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FileTest extends Activity {
private EditText editText;
private TextView showTextView;
// 要保存的文件名
private String fileName = "chenzheng_java.txt";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲取頁面中的組件
editText = (EditText) findViewById(R.id.addText);
showTextView = (TextView) findViewById(R.id.showText);
Button addButton = (Button) this.findViewById(R.id.addButton);
Button showButton = (Button) this.findViewById(R.id.showButton);
// 綁定單擊事件
addButton.setOnClickListener(listener);
showButton.setOnClickListener(listener);
}
// 聲明監(jiān)聽器
private View.OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
Button view = (Button) v;
switch (view.getId()) {
case R.id.addButton:
save();
break;
case R.id.showButton:
read();
break;
}
}
};
/**
*@author chenzheng_Java
*保存用戶輸入的內(nèi)容到文件
*/
private void save() {
String content = editText.getText().toString();
try {
/* 根據(jù)用戶提供的文件名,以及文件的應(yīng)用模式,打開一個(gè)輸出流.文件不存系統(tǒng)會(huì)為你創(chuàng)建一個(gè)的,
* 至于為什么這個(gè)地方還有FileNotFoundException拋出,我也比較納悶。在Context中是這樣定義的
* public abstract FileOutputStream openFileOutput(String name, int mode)
* throws FileNotFoundException;
* openFileOutput(String name, int mode);
* 第一個(gè)參數(shù),代表文件名稱,注意這里的文件名稱不能包括任何的/或者/這種分隔符,只能是文件名
* 該文件會(huì)被保存在/data/data/應(yīng)用名稱/files/chenzheng_java.txt
* 第二個(gè)參數(shù),代表文件的操作模式
* MODE_PRIVATE 私有(只能創(chuàng)建它的應(yīng)用訪問) 重復(fù)寫入時(shí)會(huì)文件覆蓋
* MODE_APPEND 私有 重復(fù)寫入時(shí)會(huì)在文件的末尾進(jìn)行追加,而不是覆蓋掉原來的文件
* MODE_WORLD_READABLE 公用 可讀
* MODE_WORLD_WRITEABLE 公用 可讀寫
* */
FileOutputStream outputStream = openFileOutput(fileName,
Activity.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.flush();
outputStream.close();
Toast.makeText(FileTest.this, "保存成功", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @author chenzheng_java
* 讀取剛才用戶保存的內(nèi)容
*/
private void read() {
try {
FileInputStream inputStream = this.openFileInput(fileName);
byte[] bytes = new byte[1024];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while (inputStream.read(bytes) != -1) {
arrayOutputStream.write(bytes, 0, bytes.length);
}
inputStream.close();
arrayOutputStream.close();
String content = new String(arrayOutputStream.toByteArray());
showTextView.setText(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
其他的都為默認(rèn)。
關(guān)于文件保存的路徑可以通過ADT攜帶的File Explorer工具進(jìn)行查看。如何調(diào)出File Explorer工具呢;我們可以通過Windows--showView--others-android下面看到File Explorer。這里是我的一個(gè)截圖。

對(duì)于這個(gè)程序,基本上沒什么難點(diǎn),就是純粹的java流知識(shí)。唯一不同的就是context為我們提供了兩個(gè)方法來獲取輸入輸出流。簡(jiǎn)單、方便、快捷啊。
相關(guān)文章
Android使用post方式上傳圖片到服務(wù)器的方法
這篇文章主要介紹了Android使用post方式上傳圖片到服務(wù)器的方法,結(jié)合實(shí)例形式分析了Android文件傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下2016-03-03
詳解Android的網(wǎng)絡(luò)數(shù)據(jù)存儲(chǔ)
LeanCloud是一種簡(jiǎn)單高效的數(shù)據(jù)和文件存儲(chǔ)服務(wù),本文主要介紹了利用LeanCloud來進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)的存儲(chǔ)的實(shí)現(xiàn)方法。具有很好的參考價(jià)值,需要的朋友一起來看下吧2016-12-12
Android輸入法與表情面板切換時(shí)的界面抖動(dòng)問題解決方法
這篇文章主要介紹了Android輸入法與表情面板切換時(shí)的界面抖動(dòng)問題解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android開發(fā)筆記之:深入理解Cursor相關(guān)的性能問題
本篇文章是對(duì)Android中Cursor相關(guān)的性能問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Flutter runApp GestureBinding使用介紹
這篇文章主要為大家介紹了Flutter runApp GestureBinding使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Flutter permission_handler 權(quán)限插件的使用詳解
這篇文章主要介紹了Flutter permission_handler 權(quán)限插件的使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android中RecyclerView布局代替GridView實(shí)現(xiàn)類似支付寶的界面
RecyclerView比GridView來得更加強(qiáng)大,不僅是在分割線的繪制方面,在條目的編輯上也做得同樣出色,下面就來看一下Android中RecyclerView布局代替GridView實(shí)現(xiàn)類似支付寶的界面的實(shí)例2016-06-06

