3

I access to an XML file placed in a project folder (for instance 'assets') for open and read data from file.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
InputStream iS = this.getAssets().open("myFile.xml");  
Document doc = docBuilder.parse(iS);

but If I want to edit the file, I can't find the path to save it.

TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
                //create string from xml tree
                StringWriter sw = new StringWriter();
                StreamResult result = new StreamResult(sw);
                DOMSource source = new DOMSource(doc);
                trans.transform(source, result);
                String xmlString = sw.toString();
                OutputStream f0;
            byte buf[] = xmlString.getBytes();
            **f0 = new FileOutputStream("/assets/wines.xml");**
            //f0 = new FileOutputStream("file:///assets/wines.xml");
            for(int i=0;i<buf .length;i++) {
               f0.write(buf[i]);
            }
            f0.close();
            buf = null;

I obtain always a FileNotFoundException at line:

f0 = new FileOutputStream("/assets/wines.xml");

Any ideas? Thanks!

Richad Lozano
  • 33
  • 2
  • 5

2 Answers2

1

If you are trying to edit or write file to assets or raw folder. I guess it is not possible. It cannot be done

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
0

Bettery try saving it to sd card or internal storage

Use File sdDir = [Environment.getExternalStorageDirectory()][1]; to get the sdcard path.

And don't forget to give permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Hein
  • 2,675
  • 22
  • 32