2019年2月20日 星期三

[Android]_ch7.2_internal storage:存取行動裝置的記憶體。



[Android]_ch7.2_internal storage:存取行動裝置的記憶體。



可以將檔案儲存在行動裝置的記憶體內,這種方式稱為內部儲存,說明如下
呼叫ContextopenFileOutput()可以輸出資料
呼叫FileOutputStreamwrite()資料寫入檔案
呼叫FileOutputStreamclose()關閉寫入檔案的資料串
呼叫ContextopenFileInput()可以讀取資料
呼叫FileInputStreamread()讀入檔案資料
呼叫FileInputStreamclose()關閉讀取檔案的資料串流



package com.derek.ch22_internalstorage;



import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;



import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.TextView;

import android.widget.Toast;





public class MainActivity extends Activity {

TextView tv4;

Button btn1,btn2,btn3;

EditText et1,et2;

RadioButton rb1,rb2;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        et1=(EditText)findViewById(R.id.editText1);

        et2=(EditText)findViewById(R.id.editText2);

        rb1=(RadioButton)findViewById(R.id.radioButton1);

        rb2=(RadioButton)findViewById(R.id.radioButton2);

        btn1=(Button)findViewById(R.id.button1);

        btn2=(Button)findViewById(R.id.button2);

        btn3=(Button)findViewById(R.id.button3);

        tv4=(TextView)findViewById(R.id.textView4);

        btn1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String name=et1.getText().toString();

boolean sex=(rb1.isChecked())?true:false;

int phone=Integer.parseInt(et2.getText().toString());

FileOutputStream fos = null;

try {

fos=openFileOutput("Derek.txt",MODE_PRIVATE);

fos.write(name.getBytes());

fos.write(Boolean.toString(sex).getBytes());

fos.write(Integer.toString(phone).getBytes());

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Toast.makeText(MainActivity.this, "1.存檔成功!位置:"+fos.toString(), Toast.LENGTH_SHORT).show();

}

});

        btn2.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String name=et1.getText().toString();

boolean sex=(rb1.isChecked())?true:false;

int phone=Integer.parseInt(et2.getText().toString());

FileOutputStream fos = null;

try {

fos=openFileOutput("Derek.txt",MODE_APPEND);

fos.write((name+"\n").getBytes());

fos.write((Boolean.toString(sex)+"\n").getBytes());

fos.write((Integer.toString(phone)+"\n").getBytes());

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Toast.makeText(MainActivity.this, "2.附加寫入!", Toast.LENGTH_SHORT).show();

}

});

        btn3.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {



try {

FileInputStream fis=openFileInput("Derek.txt");

InputStreamReader isr=new InputStreamReader(fis);

BufferedReader br=new BufferedReader(isr);

StringBuilder sb=new StringBuilder("讀取內容如下:\n");

String msg="";

while((msg=br.readLine())!=null){

sb.append(msg+"\n");

}

br.close();

isr.close();

fis.close();

tv4.setText(sb);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Toast.makeText(MainActivity.this, "3.讀取完畢!", Toast.LENGTH_SHORT).show();

}

});

    }



}


沒有留言:

張貼留言

Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...