Android中File形式儲存資料【安卓進化三十】

來源:互聯網
上載者:User

            前面我簡單地介紹了2種資料儲存的方式,例如:在【安卓進化十三】中介紹了Shared  Preferences,在【安卓進化十四】中我寫了個sqlite的資料庫儲存資料的通訊錄的例子,下面我介紹一下File儲存資料的形式,File形式我講兩點,一個是檔案是不能在不同的程式間共用的。另一個特點是用openFileOutput方法開啟一個檔案(如果這個檔案不存在就建立這個檔案),openFileOutput往檔案裡儲存東西的方法。我寫了個例子:點擊button,寫個int類型的數(count)在file中,然後除以2取餘數,餘數為0則狀態為關,餘數為1則狀態為開。這個count值一直++。這是個簡單的例子,為了說明file儲存資料的效果。轉載請標明出處:http://blog.csdn.net/wdaming1986/article/details/6849703

 

請看:       點擊button按鈕                                                               再次點擊button按鈕

                                                                  

 

                              這是dmfile.cfg檔案中的內容:mButton一直++,為奇數時mChange=false,為偶數時true。

                             

 

代碼奉上:在這個FileStorage工程裡面:

在com.cn.daming包裡:FileStorageMainActivity.java類中的內容:

package com.cn.daming;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class FileStorageMainActivity extends Activity {    private TextView mTextView;    private Button cButton;private boolean mChanged = false;public int mCount = 0;@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mTextView = (TextView)findViewById(R.id.show_textview);        cButton = (Button)findViewById(R.id.button);        //read the mChanged is true or false        readLoadFile();        refreTextView();        initButton();    }private void initButton(){cButton.setOnClickListener(new OnClickListener(){public void onClick(View arg0) {readLoadFile();if(mCount%2==0){mChanged = false;refreTextView();}if(mCount%2==1){mChanged = true;refreTextView();}mCount++;saveFileStorage();}});}public void refreTextView(){if(mChanged)        {        mTextView.setText("目前狀態為:開!");        }        else        {        mTextView.setText("目前狀態為:關!");        }}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode == KeyEvent.KEYCODE_BACK){//save the fileStorage before the programe backsaveFileStorage();if(mChanged){Toast.makeText(this, "程式退出狀態為開!", Toast.LENGTH_LONG).show();}else{Toast.makeText(this, "程式退出狀態為關!", Toast.LENGTH_LONG).show();}this.finish();return true;}return super.onKeyDown(keyCode, event);}    private void readLoadFile()    {    //make the Properties     Properties properties = new Properties();    try{    FileInputStream istream = this.openFileInput("dmfile.cfg");    properties.load(istream);    }    catch(FileNotFoundException e){    return;    }    catch(IOException e){    return;    }        mChanged = Boolean.valueOf(properties.getProperty("mChanging").toString());    mCount = Integer.valueOf(properties.getProperty("mButton").toString());        }        private boolean saveFileStorage()    {    Properties properties = new Properties();    properties.put("mChanging", String.valueOf(mChanged));    properties.put("mButton", String.valueOf(mCount));    try{    FileOutputStream ostream = this.openFileOutput("dmfile.cfg", Context.MODE_WORLD_WRITEABLE);    properties.store(ostream, "");    }    catch(FileNotFoundException e)    {    return false;    }    catch(IOException e)    {    return false;    }    return true;    }}
 

main.xml檔案裡面的內容:

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    android:gravity="center_vertical|center_horizontal"    android:layout_marginBottom="15dip"    android:layout_marginTop="15dip"/><Button    android:id="@+id/button"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="測試改變File content"    android:layout_marginBottom="10dip"/><TextView    android:id="@+id/show_textview"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:gravity="center"/></LinearLayout>

聯繫我們

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