android数据存储-->Files

 

2.Files:使用Properties存取数据,

使用input=openFileInput(“file.cfg”);打开文件返回FileInputStream实例

    Output=openFileOutput(“file.cfg”,MODE_WORLD_WRITEABLE);返回FileOutputStream实例

properties.load(input);读取文件

properties.store(output,””);写入文件

properties.get(“”);去的文件内容

 

例如:package cao.org.android.filesdemo;

 

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.os.Bundle;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

 

public class Files_Activity extends Activity {

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

       if(keyCode==KeyEvent.KEYCODE_BACK){

           save();

           finish();

           return true;

       }

       return super.onKeyDown(keyCode, event);

    }

    private void save(){

       Properties properties_2=new Properties();

       properties_2.put("but", string);

       try {

           FileOutputStream output=openFileOutput("file.cfg",  MODE_WORLD_WRITEABLE );

           try {

              properties_2.store(output, "");

           } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

              return;

           }

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

           return;

       }

      

    }

    private Button but_1,but_2;

    private TextView text_1;

    private String string="关闭";

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        but_1=(Button)findViewById(R.id.but_1);

        but_2=(Button)findViewById(R.id.but_2);

        text_1=(TextView)findViewById(R.id.text_1);

        but_1.setOnClickListener(but_1_listener);

        but_2.setOnClickListener(but_2_listener);

        load();

        text_1.setText("当前状态:"+string);

    }

    private void load() {

       Properties properties_1=new Properties();

      

       try {

           FileInputStream intput=this.openFileInput("file.cfg");

           try {

              properties_1.load(intput);

           } catch (IOException e) {

              // TODO Auto-generated catch block

              return;

           }

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           return;

       }

       string=properties_1.getProperty("but");

    }

    private OnClickListener but_1_listener=new OnClickListener() {

      

       @Override

       public void onClick(View v) {

           string="开启";

           text_1.setText(string);

       }

    };

    private OnClickListener but_2_listener=new OnClickListener() {

      

       @Override

       public void onClick(View v) {

           string="关闭";

           text_1.setText(string);

           

       }

    };

}

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:id="@+id/text_1"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text=""

    />

<Button

    android:id="@+id/but_1"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="开启"

    />

<Button

    android:id="@+id/but_2"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="关闭"

    />

</LinearLayout>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值