Android 新建xml文档用来保存配置信息

做安卓软件需要能够通用,可是很多机器系统都是定制的,系统权限开放也不是很多,所以有时候需要将一些信息保存在系统的应用隐藏文件夹下,xml文件很方便。

代码如下:

public void CreateXML(String filepath) {
		String dir = filepath.substring(0, filepath.lastIndexOf("/"));
		File dirfile = new File(dir);
		if(!dirfile.exists())
			dirfile.mkdirs();//创建文件夹
		
		File file = new File(filepath);
		try {
			file.createNewFile();//创建文件
		} catch (IOException e) {
			 Log.e("IOException", "exception in createNewFile() method");
			 return;
		}
		FileOutputStream fileos = null;
		try {
			fileos = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			 Log.e("CreateXML", "can't create FileOutputStream");
			 return;
		}
		XmlSerializer serializer = Xml.newSerializer();
		try {
			serializer.setOutput(fileos, "UTF-8");
			serializer.startDocument("UTF-8", true);

			serializer.startTag("", "SettingInfo");

			serializer.startTag("", "XX1");
			serializer.text("1");
			serializer.endTag("", "XX1");

			serializer.startTag("", "XX2");
			serializer.text("1");
			serializer.endTag("", "XX2");

			serializer.startTag("", "XX3");
			serializer.text("1");
			serializer.endTag("", "XX3");

			serializer.startTag("", "XX4");
			serializer.text("0");
			serializer.endTag("", "XX4");

			serializer.startTag("", "XX5");
			serializer.text("1");
			serializer.endTag("", "XX5");

			serializer.endTag("", "SettingInfo");

			serializer.endDocument();
			serializer.flush();
			fileos.close();
		} catch (Exception e) {
			 Log.e("CreateXML","error occurred while creating xml file");
			 return;
		}
		Log.e("CreateXML","Create XML FILE SUCESSEd");
	} 


你可能感兴趣的:(android,Android,创建xml文件,用xml文件保存信息)