android数据存储-->Files
来源:互联网 发布:淘宝上卖烟的店铺代号 编辑:程序博客网 时间:2024/05/20 08:24
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>
- android数据存储-->Files
- 【Android】数据存储之Files
- Android数据存储(二) Files
- Android数据存储之间Files
- Android数据持久化存储-Files对象
- Android 数据存储——Saving Files
- Android 小项目之--数据存储【Files】
- Android 小项目之--数据存储【Files】(附源码)
- Android 小项目之--数据存储【Files】(附源码)
- 使用Files对象存储数据
- 【android学习之九】——数据存储1:SharedPreferences,Files,网络
- Android存储数据的几种方式--Shared preference和files
- Android数据存取之Files
- Android 数据存取之Files
- Android | Android数据存储
- Android数据存储--网络存储
- Android数据存储:File存储
- Android 数据存储--文件存储
- 第五周任务一之构造函数的参数初始化表
- RPM与SRPM
- u-boot走读 -- start.S
- 软件保护壳专题 - 代码乱序引擎的构建
- Intent 的用法
- android数据存储-->Files
- 几何拓扑介绍
- [转]Python中RE模块的应用
- iphone——Thread,RunLoop初识
- 如何在SAP的的凭证流浏览器(DRB)中显示客户自定义的凭证
- impress掩饰(PPT)框架
- 关于android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an app
- 成员函数不能作为pthread——create的参数
- 嵌入式学习网站综合