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

首頁 > 系統 > Android > 正文

Android編程中File文件常見存儲與讀取操作demo示例

2019-12-12 01:59:48
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程中File文件常見存儲與讀取操作。分享給大家供大家參考,具體如下:

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());    // 獲取兩個按鈕    Button read = (Button) findViewById(R.id.read);    Button write = (Button) findViewById(R.id.write);    // 獲取兩個文本框    final EditText edit1 = (EditText) findViewById(R.id.edit1);    final EditText edit2 = (EditText) findViewById(R.id.edit2);    // 為write按鈕綁定事件監聽器    write.setOnClickListener(new View.OnClickListener()    {      @Override      public void onClick(View source)      {        // 將edit1中的內容寫入文件中        write(edit1.getText().toString());        edit1.setText("");      }    });    read.setOnClickListener(new View.OnClickListener()    {      @Override      public void onClick(View v)      {        // 讀取指定文件中的內容,并顯示出來        edit2.setText(read());      }    });  }  private String read()  {    try    {      // 打開文件輸入流      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    {      // 以追加模式打開文件輸出流      FileOutputStream fos = openFileOutput(FILE_NAME, MODE_APPEND);      // 將FileOutputStream包裝成PrintStream      PrintStream ps = new PrintStream(fos);      // 輸出文件內容      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>

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android布局layout技巧總結》、《Android開發入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴楚县| 松滋市| 调兵山市| 广西| 文昌市| 星座| 渑池县| 华蓥市| 周口市| 新丰县| 都江堰市| 平定县| 台南市| 洛南县| 高雄县| 始兴县| 肇州县| 刚察县| 桐乡市| 武胜县| 志丹县| 深州市| 九龙坡区| 于都县| 仪陇县| 中江县| 蕲春县| 潮安县| 阿坝县| 普宁市| 富阳市| 建始县| 望都县| 门头沟区| 买车| 榆社县| 汾阳市| 颍上县| 祁阳县| 都昌县| 常山县|