1. 程式人生 > >Android之內部儲存讀取資料

Android之內部儲存讀取資料

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <EditText
            android:id="@+id/et"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="5"
            android:hint="請輸入資料"
            android:singleLine="true" />


        <Button
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="3"
            android:onClick="click"
            android:text="儲存資料" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <TextView
            android:id="@+id/tv"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="5"
            android:hint="輸出資料"
            android:singleLine="true" />


        <Button
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="3"
            android:onClick="click2"
            android:text="讀取資料" />
    </LinearLayout>


</LinearLayout>


xml中佈局,設定儲存讀取按鈕。

public class MainActivity extends Activity {


private EditText text;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


text = (EditText) findViewById(R.id.et);
}


public void click(View v) {


Toast.makeText(this, "儲存資料中", 1).show();


String data = text.getText().toString();


save(data, "test.txt");


}


// 儲存資料data到filename中
private void save(String data, String filename) {
// TODO Auto-generated method stub


File file = new File(getFilesDir(), filename);


if (file.exists()) {


FileOutputStream output;


try {
output = new FileOutputStream(file);


output.write(data.getBytes());


output.close();


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



public void click2(View v) {
// TODO Auto-generated method stub


TextView tv = (TextView) findViewById(R.id.tv);
Toast.makeText(this, "讀取資料中", 1).show();


String tv_data = read( );

Log.d("Qishui",tv_data);
tv.setText(tv_data);


}


private String read() {
// TODO Auto-generated method stub
File file = new File(getFilesDir(), "test.txt");


String data = null;


if (file.exists()) {


FileInputStream input;


try {
input = new FileInputStream(file);


BufferedReader br = new BufferedReader(new InputStreamReader(input));


data = br.readLine();


input.close();


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return data;


}


}



注意:不需要許可權!