Android編程中File文件常見(jiàn)存儲(chǔ)與讀取操作demo示例
本文實(shí)例講述了Android編程中File文件常見(jiàn)存儲(chǔ)與讀取操作。分享給大家供大家參考,具體如下:
MainActivity文件代碼如下:
package example.com.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.PrintStream; public class MainActivity extends Activity { final String FILE_NAME = "test.txt"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.out.println(new StringBuilder("a").append("b").append("c") .toString()); // 獲取兩個(gè)按鈕 Button read = (Button) findViewById(R.id.read); Button write = (Button) findViewById(R.id.write); // 獲取兩個(gè)文本框 final EditText edit1 = (EditText) findViewById(R.id.edit1); final EditText edit2 = (EditText) findViewById(R.id.edit2); // 為write按鈕綁定事件監(jiān)聽(tīng)器 write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View source) { // 將edit1中的內(nèi)容寫(xiě)入文件中 write(edit1.getText().toString()); edit1.setText(""); } }); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 讀取指定文件中的內(nèi)容,并顯示出來(lái) edit2.setText(read()); } }); } private String read() { try { // 打開(kāi)文件輸入流 FileInputStream fis = openFileInput(FILE_NAME); byte[] buff = new byte[1024]; int hasRead = 0; StringBuilder sb = new StringBuilder(""); while ((hasRead = fis.read(buff)) > 0) { sb.append(new String(buff, 0, hasRead)); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } private void write(String content) { try { // 以追加模式打開(kāi)文件輸出流 FileOutputStream fos = openFileOutput(FILE_NAME, MODE_APPEND); // 將FileOutputStream包裝成PrintStream PrintStream ps = new PrintStream(fos); // 輸出文件內(nèi)容 ps.println(content); ps.close(); } catch (Exception e) { e.printStackTrace(); } } }
布局文件代碼如下:
<!--?xml version="1.0" encoding="utf-8"?--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/edit1" android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="4"/> <Button android:id="@+id/write" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="write"/> <EditText android:id="@+id/edit2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:lines="4"/> <Button android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="read"/> </LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android開(kāi)發(fā)實(shí)現(xiàn)文件存儲(chǔ)功能
- Android存儲(chǔ)字符串?dāng)?shù)據(jù)到txt文件
- Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
- Android開(kāi)發(fā)實(shí)現(xiàn)讀取Assets下文件及文件寫(xiě)入存儲(chǔ)卡的方法
- Android用文件存儲(chǔ)數(shù)據(jù)的方法
- Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)
- Android實(shí)現(xiàn)文件存儲(chǔ)并讀取的示例代碼
- Android 文件數(shù)據(jù)存儲(chǔ)實(shí)例詳解
- 詳解Android數(shù)據(jù)存儲(chǔ)之Android 6.0運(yùn)行時(shí)權(quán)限下文件存儲(chǔ)的思考
- android數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)方法
- 詳解Android 中的文件存儲(chǔ)
相關(guān)文章
關(guān)于Android Studio封裝SDK的那些事兒
這篇文章主要給大家介紹了關(guān)于Android Studio封裝SDK的那些事兒,文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Android小知識(shí)之圖片的3種壓縮方式小結(jié)
這篇文章主要給大家介紹了關(guān)于Android小知識(shí)之圖片的3種壓縮方式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10Android抓取CSDN首頁(yè)極客頭條內(nèi)容完整實(shí)例
這篇文章主要介紹了Android抓取CSDN首頁(yè)極客頭條內(nèi)容完整實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01基于Android中手勢(shì)交互的實(shí)現(xiàn)方法
本篇文章是對(duì)Android中手勢(shì)交互的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05Android沉浸式頂部實(shí)現(xiàn)代碼及效果
這篇文章主要介紹了Android沉浸式頂部實(shí)現(xiàn)代碼及效果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Android手勢(shì)密碼view學(xué)習(xí)筆記(一)
這篇文章主要為大家詳細(xì)介紹了Android手勢(shì)密碼view的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03詳解Android開(kāi)發(fā)中Activity的四種launchMode
這篇文章主要介紹了Android開(kāi)發(fā)中Activity的四種launchMode,launchMode主要用于控制多個(gè)Activity間的跳轉(zhuǎn),需要的朋友可以參考下2016-03-03Android NavigationView頭部設(shè)置監(jiān)聽(tīng)事件
這篇文章主要為大家詳細(xì)介紹了Android NavigationView頭部設(shè)置監(jiān)聽(tīng)事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10