0

I have written the code below, but I keep receiving errors about Path Separator.

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();



        }
    });

}

}

I need to open a file in my SDCard, but i can't open it. The purpose is to add new records to an existing XML file, but since i can't read it, i can't add the new record. I have another method that create the XML file and add the record, and it works, but this one won't work.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Luís Assunção
  • 19
  • 1
  • 1
  • 8
  • Please post a Logcat. – Prmths Sep 17 '13 at 15:35
  • Here it is:http://img585.imageshack.us/img585/272/gmb9.png When i create the file and add the record, it works, but i have this code that only adds the new data since the file already exists, dont give any erros, only this "Path Separator" but the data is lost. – Luís Assunção Sep 17 '13 at 15:48

1 Answers1

3

context.openFileOutput: Direct from DOC: is used to Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.

This means your are trying to open a file that is not on the SDcard.

To open a file that is on the External Storage or the SD card:

 File root = new File(Environment.getExternalStorageDirectory(), "Your folder");
 File gpxfile = new File(root, "xml_nova_contagem.xml");

Remember that if your app is working with the SD card you need to manage the scenarios where the SD card is not mounted, etc. look this : Writing Text File to SD Card fails

If your xml document works as database, or you are going to save critical information here, I suggest that you should create this xml file in the Internal Storage :

http://developer.android.com/guide/topics/data/data-storage.html

Community
  • 1
  • 1
Tobiel
  • 1,543
  • 12
  • 19
  • The problem here is that he "can" read the file, and "write" the new data, but the new data won't appear in the file. Seems that the data is lost in the process. – Luís Assunção Sep 17 '13 at 16:16
  • if you can "read" the file and "write" the file with out any error, how you can loose your data? there is no way. Also he say that is trying to open a file from the SDcard but in your code, you are trying to open a file on the internal storage. – Tobiel Sep 17 '13 at 16:23
  • File file = new File("xml_novo.xml",PATH ); the first parameter should be the path of the directory, and the second the name of the file, and you don't need to add the "\" – Tobiel Sep 17 '13 at 17:03