我已经编写了下面的代码,但我不断收到有关路径分隔符的错误。
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)
{
// If the file don't exists
File file = new File(Environment.getExternalStorageDirectory().toString()+"/xml_nova_contagem.xml" );
fileos = new FileOutputStream(file);
}
// If the file exists
/////////-------- THIS METHOD WON'T WORK --------///////
/* if (fileos!=null){
System.out.println("aqui vai dar bronca: " + Environment.getExternalStorageDirectory().toString());
// PATH = Environment.getExternalStorageDirectory().toString() + "/xml_nova_contagem.xml";
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, "CodigoPostal");
serializer.startTag(null, "CP7");
serializer.text(CP7_String);
serializer.endTag(null,"CP7");
serializer.startTag(null,"NovaContagem");
serializer.text(ncontagem_String);
serializer.endTag(null, "NovaContagem");
serializer.endTag(null, "CodigoPostal");
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();
}
});
}
}
我需要在我的 SDCard 中打开一个文件,但我无法打开它。目的是将新记录添加到现有 XML 文件中,但由于我无法读取它,因此无法添加新记录。我有另一种创建 XML 文件并添加记录的方法,它可以工作,但是这个不行。