我写了这段代码
btn_guardar.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
final String CP7_String = et_id.getText().toString();
final String ncontagem_String = et_ncontagem.getText().toString();
Thread thread = new Thread () {
public void run() {
String PATH=null ;
FileOutputStream fileos = null;
try{
if(fileos==null)
{
//////// Create and add ///////
// If the XML don't exists, then create
File file = new File("/mnt/sdcard/xml_nova_contagem.xml");
fileos = new FileOutputStream(file);
};
if (fileos!=null){
//////// Add ONLY ///////
// If the file exists, then add the new values
System.out.println("aqui vai dar bronca: " + Environment.getExternalStorageDirectory().toString());
Context context= getApplicationContext();
fileos = context.openFileOutput("xml_nova_contagem.xml", Context.MODE_APPEND);
System.out.println("okkkkkkkkk ");
};
XmlSerializer serializer = Xml.newSerializer();
try{
serializer.setOutput(fileos, "iso-8859-1");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.startTag(null,"Contagem_URL id= ");
serializer.startTag(null, "Contagem");
serializer.startTag(null, "ID");
serializer.text(CP7_String);
serializer.endTag(null,"ID");
serializer.startTag(null,"NContagem");
serializer.text(ncontagem_String);
serializer.endTag(null, "NContagem");
serializer.endTag(null, "Contagem");
serializer.startTag(null,"Contagem_URL id= ");
serializer.endDocument();
serializer.flush();
fileos.close();
System.out.println("endddddddddda");
}catch(Exception ee)
{
System.out.println("Exception"+ee);
}
}
catch (Exception e) {
System.out.println("Erro a escrever"+e);
System.out.println("path"+PATH);
}
}
};thread.start();
}
});
我可以创建一个XML文件并添加一个ID和一个Contagem。但是,如果文件已经存在,我无法添加新数据。如果我只运行“创建并添加”,它运行正常。但是如果我在XML文件存在时运行“Add Only”,它将不会添加新数据。如果我同时运行这些代码,则XML保持为空。
有人能帮助我吗? 它是我项目毕业时唯一遗漏的东西。