android 40 Io編程

來源:互聯網
上載者:User

標籤:

Io編程:記憶卡和sd卡。字串存入記憶卡然後讀出來。

activity:

package com.sxt.day06_06;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {    EditText met;//文字框    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        setListener();    }    private void setListener() {        setSaveDataClickListener();        setReadDataClickListener();    }    private void setReadDataClickListener() {        findViewById(R.id.btnReadData).setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                FileInputStream in =null;                try {                    in= openFileInput("file.dat");                    byte[] data=new byte[1024];                    int len = in.read(data);//返回實際讀取的位元組數                    String text=new String(data, 0, len, "utf-8");                    Toast.makeText(MainActivity.this, text, 3000).show();                } catch (FileNotFoundException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }finally{                    if(in!=null){                        try {                            in.close();                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                }            }        });    }    private void setSaveDataClickListener() {        findViewById(R.id.btnSaveData).setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                FileOutputStream out = null;                try {                    out=openFileOutput("file.dat", MODE_PRIVATE);                    String text=met.getText().toString();                    byte[] data=text.getBytes("utf-8");//FileOutputStream時data要轉換為位元組數組                    out.write(data);                    out.flush();                } catch (FileNotFoundException e) {                    e.printStackTrace();                } catch (UnsupportedEncodingException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }finally{                    if(out!=null){                        try {                            out.close();                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                }            }        });    }    private void initView() {        met=(EditText) findViewById(R.id.et);    }}

頁面

<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/et"           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:text="哈羅,張飛!"/>    <Button        android:id="@+id/btnSaveData"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="儲存資料" />    <Button        android:id="@+id/btnReadData"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="讀取資料" />    </LinearLayout>

 

android 40 Io編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.