0

我写了这段代码

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 文件存在时运行“仅添加”,它不会添加新数据。如果我运行这两个代码,XML 保持为空。

有人能帮我吗?这是我项目毕业时唯一错过的东西。

4

0 回答 0