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>