在Android我们可以通过shareprefereces来处理配置文件的数据
package com.android;
import android.R.string;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class prefereceTest extends Activity {
/** Called when the activity is first created. */
int i=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建SharePrefereces
SharedPreferences sp = getSharedPreferences("myPref",MODE_WORLD_READABLE);//第一个参数为文件名,第二个为数据操作模式
Editor editor = sp.edit();
//添加数据
editor.putString("name", "mypreferece");
editor.putInt("int", 5566);
//保存数据
editor.commit();
Button bt = (Button)findViewById(R.id.bt_1);
bt.setOnClickListener(new onclicklistener());
}
class onclicklistener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//创建SharePrefereces
SharedPreferences sp = getSharedPreferences("myPref",MODE_WORLD_READABLE);
//获取数据后面一个参数表示无返回时的默认值
String defaultval = "defaultval";
String name = sp.getString("name", defaultval);
int defint =0;
int age = sp.getInt("int", defint);
TextView tv = (TextView) findViewById(R.id.tv);
//toasst显示数据
Toast.makeText(getApplicationContext(), age + " "+name,Toast.LENGTH_SHORT).show();
}
}
文件位于 、data/data/包名/shared_pref/下
}