序列化编程,保存和读取配置文件
package com.example.login;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_loginName;
private EditText et_psw;
private CheckBox cb_isAto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
// 获取控件对象
et_loginName = findViewById(R.id.et_loginName);
et_psw = findViewById(R.id.et_psw);
cb_isAto = findViewById(R.id.cb_isAto);
try {
List<ini_inf> r_lists=readinf();
for (ini_inf ini_inf : r_lists) {
System.out.println(ini_inf);
et_loginName.setText(ini_inf.getS_Name());
et_psw.setText(ini_inf.getS_Psw());
cb_isAto.setChecked("true".equals(ini_inf.getS_IsCheck()) ?true:false);
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public void Login(View v) throws IOException {
// 获取账号密码的值
String LoginName = et_loginName.getText().toString().trim();
String psw = et_psw.getText().toString().trim();
String path;
if (LoginName.isEmpty() || psw.isEmpty()) {
Toast.makeText(MainActivity.this, "账号和密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
// Toast.makeText(MainActivity.this, LoginName+psw, Toast.LENGTH_SHORT).show();
if (cb_isAto.isChecked()) {
// 首先获取SDK是否挂载
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
// 是的话就获取SDK的路径
path = Environment.getExternalStorageDirectory().getPath();
} else {
// 否则的话就获取手机内部存储路径
path = MainActivity.this.getFilesDir().getPath();
}
// 组合配置文件
List<ini_inf> l_Ini_infs = new ArrayList<ini_inf>();
ini_inf b_Ini_inf = new ini_inf();
b_Ini_inf.setS_IsCheck(cb_isAto.isChecked() ? "true" : "false");
b_Ini_inf.setS_Name(LoginName);
b_Ini_inf.setS_Psw(psw);
l_Ini_infs.add(b_Ini_inf);
// 创建输出流
File file = new File(path, "login.xml");
FileOutputStream fo = new FileOutputStream(file);
// 获取XmlSerializer对象
XmlSerializer p_Serializer = Xml.newSerializer();
// 设置XmlSerializer的输出流和编码方式
p_Serializer.setOutput(fo, "utf-8");
p_Serializer.startDocument("utf-8", true);
p_Serializer.startTag(null, "配置清单");// 设置头节点
for (ini_inf ini_inf : l_Ini_infs) {
// 组合配置信息
p_Serializer.startTag(null, "LoginName");
p_Serializer.text(ini_inf.getS_Name());
p_Serializer.endTag(null, "LoginName");
p_Serializer.startTag(null, "psw");
p_Serializer.text(ini_inf.getS_Psw());
p_Serializer.endTag(null, "psw");
p_Serializer.startTag(null, "ischeck");
p_Serializer.text(ini_inf.getS_IsCheck());
p_Serializer.endTag(null, "ischeck");
}
p_Serializer.endTag(null, "配置清单");
p_Serializer.endDocument();
fo.close();
}
}
public void read_ini(View v) {
// TODO 自动生成的方法存根
try {
List<ini_inf> r_lists=readinf();
if(r_lists==null)
{
System.out.println("返回为空");
return;
}
for (ini_inf ini_inf : r_lists) {
System.out.println(ini_inf);
et_loginName.setText(ini_inf.getS_Name());
et_psw.setText(ini_inf.getS_Psw());
cb_isAto.setChecked("true".equals(ini_inf.getS_IsCheck()) ?true:false);
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public List<ini_inf> readinf() throws Exception {
List<ini_inf> r_lists = null;
ini_inf p_ini=null;
String path;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
path = Environment.getExternalStorageDirectory().getPath();
} else {
path = MainActivity.this.getFilesDir().getPath();
}
System.out.println("path:"+path);
File file = new File(path, "login.xml");
// 获取XmlPullParser 对象
FileInputStream fin = new FileInputStream(file);
XmlPullParser xpp = Xml.newPullParser();
// 设置 XmlPullParser的参数
xpp.setInput(fin, "utf-8");
// 获取XmlPullParser的事件类型
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
// 如果事件类型不是xml的结束文档类型
System.out.println("eventType:"+eventType);
switch (eventType) {
case XmlPullParser.START_TAG:
if ("配置清单".equals(xpp.getName())) {
//创建集合对象
r_lists=new ArrayList<ini_inf>();
System.out.println("创建集合对象");
}else if ("LoginName".equals(xpp.getName())) {
p_ini=new ini_inf();
p_ini.setS_Name(xpp.nextText());
System.out.println("创建小对象设置账号信息");
} else if ("psw".equals(xpp.getName())) {
p_ini.setS_Psw(xpp.nextText());
}else if ("ischeck".equals(xpp.getName())) {
p_ini.setS_IsCheck(xpp.nextText());
r_lists.add(p_ini);
}
break;
case XmlPullParser.END_TAG:
if ("ischeck".equals(xpp.getName())) {
}
break;
}
//获取下个数据
eventType = xpp.next();
}
fin.close();
return r_lists;
}
}