国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android應用程序中讀寫txt文本文件的基本方法講解

2019-12-12 06:37:05
字體:
來源:轉載
供稿:網友

2016412163511854.gif (370×345)

最終效果圖,點擊save會保存到文件中,點擊show會從文件中讀取出內容并顯示。

2016412163541130.gif (235×373)

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="請您輸入要保存的內容:"   />  <EditText   android:id="@+id/addText"   android:layout_width="fill_parent"    android:layout_height="wrap_content"   android:hint="請您在此處輸入文件內容!"  />    <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);    }    // 聲明監聽器   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    *保存用戶輸入的內容到文件    */   private void save() {      String content = editText.getText().toString();     try {       /* 根據用戶提供的文件名,以及文件的應用模式,打開一個輸出流.文件不存系統會為你創建一個的,        * 至于為什么這個地方還有FileNotFoundException拋出,我也比較納悶。在Context中是這樣定義的        *  public abstract FileOutputStream openFileOutput(String name, int mode)        *  throws FileNotFoundException;        * openFileOutput(String name, int mode);        * 第一個參數,代表文件名稱,注意這里的文件名稱不能包括任何的/或者/這種分隔符,只能是文件名        *     該文件會被保存在/data/data/應用名稱/files/chenzheng_java.txt        * 第二個參數,代表文件的操作模式        *     MODE_PRIVATE 私有(只能創建它的應用訪問) 重復寫入時會文件覆蓋        *     MODE_APPEND 私有  重復寫入時會在文件的末尾進行追加,而不是覆蓋掉原來的文件        *     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    * 讀取剛才用戶保存的內容    */   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();     }    }  } 

其他的都為默認。

關于文件保存的路徑可以通過ADT攜帶的File Explorer工具進行查看。如何調出File Explorer工具呢;我們可以通過Windows--showView--others-android下面看到File Explorer。這里是我的一個截圖。

2016412163634268.gif (776×235)

對于這個程序,基本上沒什么難點,就是純粹的java流知識。唯一不同的就是context為我們提供了兩個方法來獲取輸入輸出流。簡單、方便、快捷啊。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高碑店市| 修水县| 北票市| 拉萨市| 屯留县| 且末县| 游戏| 上蔡县| 绍兴市| 周宁县| 阜阳市| 确山县| 札达县| 龙口市| 邮箱| 岳西县| 秦安县| 三明市| 吴川市| 吉木乃县| 藁城市| 遂川县| 赫章县| 安泽县| 江山市| 砚山县| 和硕县| 衡阳县| 苏尼特右旗| 佛山市| 临沂市| 博乐市| 连城县| 广元市| 靖江市| 江阴市| 互助| 北海市| 宁津县| 红原县| 惠安县|