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

首頁(yè) > 系統(tǒng) > Android > 正文

Android編程中File文件常見(jiàn)存儲(chǔ)與讀取操作demo示例

2019-10-22 18:27:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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>

 

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵武市| 岳西县| 天峻县| 文化| 利川市| 灵璧县| 铜川市| 枞阳县| 花莲市| 综艺| 闵行区| 玛沁县| 峡江县| 镇坪县| 滦平县| 新昌县| 揭西县| 镇原县| 古浪县| 永丰县| 祁连县| 白朗县| 嘉鱼县| 景洪市| 内江市| 庄浪县| 彩票| 南召县| 梁山县| 嘉义县| 丰城市| 芒康县| 三门峡市| 随州市| 白城市| 麦盖提县| 肇庆市| 襄汾县| 英山县| 兴业县| 嘉峪关市|