Android中File形式保存数据【安卓进化三十】

            前面我简单地介绍了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("当前状态为:关!");
        }
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			//save the fileStorage before the programe back
			saveFileStorage();
			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>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值