2011-07-26 17 views
0

我正在嘗試寫入Android設備上/ res/xml目錄中的現有XML文件。兩個問題:在Android中寫入現有資源XML文件

1)這些文件是否可以被覆蓋? 2)如果是這樣,我怎樣才能獲得該文件的句柄?

我想這無濟於事:

FileOutputStream fos; 

    try { 
     fos = openFileOutput("/res/xml/scores.xml", Context.MODE_PRIVATE); 
     fos.write(writer.toString().getBytes()); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

UPDATE

嘗試使用從文件中讀取:

.... 
     PriorityQueue<Score> scores = new PriorityQueue<Score>(); 
    XmlPullParserFactory xmlFac = XmlPullParserFactory.newInstance(); 
    XmlPullParser qXML = xmlFac.newPullParser(); 
    File scoresFile = new File(c.getExternalFilesDir(null), "scores.xml"); 
    InputStream is = new FileInputStream(scoresFile); 
    qXML.setInput(is,null); 
.... 

試圖寫入使用文件:

.... 
    File scoresFile = new File(getExternalFilesDir(null), "scores.xml"); 
    OutputStream os = new FileOutputStream(scoresFile); 

    os.write(writer.toString().getBytes()); 
    os.flush(); 
    os.close(); 
.... 
+2

老實說,如果你正在試圖做到這一點,那麼你應該想到另一種方法。 –

回答

1

您n在設備上使用external storage。你不能也不應該寫入資源。而是將資源複製到外部存儲器,然後在那裏修改它。

編輯:您還可以使用應用程序的internal data folder保存文件,但再次,你會想,如果你想修改它那裏複製你的資源。這將允許文件保持在您的應用程序內部。

+0

如果您不介意設備上的任何其他應用程序能夠查看,讀取並可能修改該文件,則只能使用外部存儲。 – adamp

+0

這是我懷疑的。謝謝。 – jcampos8782

+0

我可以在這些區域的xml文件上使用XmlResourceParser嗎?這個班名中間的「資源」部分讓我覺得沒有。 – jcampos8782