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

最終效果圖,點(diǎn)擊save會保存到文件中,點(diǎn)擊show會從文件中讀取出內(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="請您輸入要保存的內(nèi)容:" /> <EditText android:id="@+id/addText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="請您在此處輸入文件內(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)用模式,打開一個輸出流.文件不存系統(tǒng)會為你創(chuàng)建一個的,
* 至于為什么這個地方還有FileNotFoundException拋出,我也比較納悶。在Context中是這樣定義的
* public abstract FileOutputStream openFileOutput(String name, int mode)
* throws FileNotFoundException;
* openFileOutput(String name, int mode);
* 第一個參數(shù),代表文件名稱,注意這里的文件名稱不能包括任何的/或者/這種分隔符,只能是文件名
* 該文件會被保存在/data/data/應(yīng)用名稱/files/chenzheng_java.txt
* 第二個參數(shù),代表文件的操作模式
* MODE_PRIVATE 私有(只能創(chuàng)建它的應(yīng)用訪問) 重復(fù)寫入時會文件覆蓋
* MODE_APPEND 私有 重復(fù)寫入時會在文件的末尾進(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。這里是我的一個截圖。

對于這個程序,基本上沒什么難點(diǎn),就是純粹的java流知識。唯一不同的就是context為我們提供了兩個方法來獲取輸入輸出流。簡單、方便、快捷啊。
相關(guān)文章
ViewPager和SlidingPaneLayout的滑動事件沖突解決方法
下面小編就為大家分享一篇ViewPager和SlidingPaneLayout的滑動事件沖突解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android實(shí)現(xiàn)字母導(dǎo)航控件的示例代碼
這篇文章主要介紹了通過自定義View實(shí)現(xiàn)字母導(dǎo)航控件的示例代碼,文中的實(shí)現(xiàn)過程講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,感興趣的可以學(xué)習(xí)一下2022-01-01
android Gallery組件實(shí)現(xiàn)的iPhone圖片滑動效果實(shí)例
這篇文章主要介紹了android Gallery組件實(shí)現(xiàn)的iPhone圖片滑動效果實(shí)例,即相冊內(nèi)的圖片實(shí)現(xiàn)可左右滑動的效果,需要的朋友可以參考下2014-07-07
Android拍照保存在系統(tǒng)相冊不顯示的問題解決方法
我們保存相冊到Android手機(jī)的時候,然后去打開系統(tǒng)圖庫找不到我們想要的那張圖片,那是因?yàn)槲覀儾迦氲膱D片還沒有更新的緣故,下面與大家分享下此問題的解決方法2013-06-06
OKhttp攔截器實(shí)現(xiàn)實(shí)踐環(huán)節(jié)源碼解析
這篇文章主要為大家介紹了OKhttp攔截器實(shí)現(xiàn)實(shí)踐環(huán)節(jié)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
最常見的猜拳小游戲Android代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了最常見的猜拳小游戲Android代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08

