Android編程中File文件常見存儲(chǔ)與讀取操作的示例分析-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)Android編程中File文件常見存儲(chǔ)與讀取操作的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

岳陽網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,岳陽網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為岳陽1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的岳陽做網(wǎng)站的公司定做!

具體如下:

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)聽器
    write.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View source)
      {
        // 將edit1中的內(nèi)容寫入文件中
        write(edit1.getText().toString());
        edit1.setText("");
      }
    });
    read.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        // 讀取指定文件中的內(nèi)容,并顯示出來
        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);
      // 輸出文件內(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編程中File文件常見存儲(chǔ)與讀取操作的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

分享文章:Android編程中File文件常見存儲(chǔ)與讀取操作的示例分析-創(chuàng)新互聯(lián)
新聞來源:http://muchs.cn/article26/dsiscg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航微信公眾號(hào)、手機(jī)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名