android檔案的寫入與讀取—簡單的文本讀寫context.openFileInput() context.openFileOutput()

來源:互聯網
上載者:User

最終,點擊save會儲存到檔案中,點擊show會從檔案中讀取出內容並顯示。

main.xml

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="請您輸入要儲存的內容:"<br /> /><br /> <EditText<br /> android:id="@+id/addText"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:hint="請您在此處輸入檔案內容!"<br /> /><br /> <Button<br /> android:id="@+id/addButton"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="save"<br /> /><br /> <Button<br /> android:id="@+id/showButton"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="show"<br /> /><br /> <TextView<br /> android:id="@+id/showText"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> /></p><p></LinearLayout><br />

 

 

activity代碼

 

package cn.com.file;</p><p>import java.io.ByteArrayOutputStream;<br />import java.io.FileInputStream;<br />import java.io.FileNotFoundException;<br />import java.io.FileOutputStream;<br />import java.io.IOException;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.EditText;<br />import android.widget.TextView;<br />import android.widget.Toast;</p><p>public class FileTest extends Activity {<br />private EditText editText;<br />private TextView showTextView;<br />// 要儲存的檔案名稱<br />private String fileName = "chenzheng_java.txt";</p><p>@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);<br />// 擷取頁面中的組件<br />editText = (EditText) findViewById(R.id.addText);<br />showTextView = (TextView) findViewById(R.id.showText);<br />Button addButton = (Button) this.findViewById(R.id.addButton);<br />Button showButton = (Button) this.findViewById(R.id.showButton);<br />// 綁定單擊事件<br />addButton.setOnClickListener(listener);<br />showButton.setOnClickListener(listener);</p><p>}</p><p>// 聲明監聽器<br />private View.OnClickListener listener = new OnClickListener() {<br />public void onClick(View v) {<br />Button view = (Button) v;<br />switch (view.getId()) {<br />case R.id.addButton:<br />save();<br />break;<br />case R.id.showButton:<br />read();<br />break;</p><p>}</p><p>}</p><p>};</p><p>/**<br /> *@author chenzheng_Java<br /> *儲存使用者輸入的內容到檔案<br /> */<br />private void save() {</p><p>String content = editText.getText().toString();<br />try {<br />/* 根據使用者提供的檔案名稱,以及檔案的應用模式,開啟一個輸出資料流.檔案不存系統會為你建立一個的,<br /> * 至於為什麼這個地方還有FileNotFoundException拋出,我也比較納悶。在Context中是這樣定義的<br /> * public abstract FileOutputStream openFileOutput(String name, int mode)<br /> * throws FileNotFoundException;<br /> * openFileOutput(String name, int mode);<br /> * 第一個參數,代表檔案名稱,注意這裡的檔案名稱不能包括任何的/或者/這種分隔字元,只能是檔案名稱<br /> * 該檔案會被儲存在/data/data/應用程式名稱/files/chenzheng_java.txt<br /> * 第二個參數,代表檔案的操作模式<br /> * MODE_PRIVATE 私人(只能建立它的應用訪問) 重複寫入時會檔案覆蓋<br /> * MODE_APPEND 私人 重複寫入時會在檔案的末尾進行追加,而不是覆蓋掉原來的檔案<br /> * MODE_WORLD_READABLE 公用 可讀<br /> * MODE_WORLD_WRITEABLE 公用 可讀寫<br /> * */<br />FileOutputStream outputStream = openFileOutput(fileName,<br />Activity.MODE_PRIVATE);<br />outputStream.write(content.getBytes());<br />outputStream.flush();<br />outputStream.close();<br />Toast.makeText(FileTest.this, "儲存成功", Toast.LENGTH_LONG).show();<br />} catch (FileNotFoundException e) {<br />e.printStackTrace();<br />} catch (IOException e) {<br />e.printStackTrace();<br />}</p><p>}</p><p>/**<br /> * @author chenzheng_java<br /> * 讀取剛才使用者儲存的內容<br /> */<br />private void read() {<br />try {<br />FileInputStream inputStream = this.openFileInput(fileName);<br />byte[] bytes = new byte[1024];<br />ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();<br />while (inputStream.read(bytes) != -1) {<br />arrayOutputStream.write(bytes, 0, bytes.length);<br />}<br />inputStream.close();<br />arrayOutputStream.close();<br />String content = new String(arrayOutputStream.toByteArray());<br />showTextView.setText(content);</p><p>} catch (FileNotFoundException e) {<br />e.printStackTrace();<br />} catch (IOException e) {<br />e.printStackTrace();<br />}</p><p>}</p><p>}

其他的都為預設。

關於檔案儲存的路徑可以通過ADT攜帶的File Explorer工具進行查看。如何調出File Explorer工具呢;我們可以通過Windows--showView--others-android下面看到File Explorer。這裡是我的一個。

 

對於這個程式,基本上沒什麼痛點,就是純粹的java流知識。唯一不同的就是context為我們提供了兩個方法來擷取輸入輸出資料流。簡單、方便、快捷啊。

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.