I am trying to save an xml file into Android's internal storage. However, the file is not getting preserved. Every time I restart the emulator, the file would be empty. The file would only get stuff written to it when I manually choose to write stuff to it within the app. Then, if I close the app or whatever, the file contents get preserved. However, when I shutdown the emulator, the file would be there, but empty.
Therefore, I am not sure if I am correctly saving it.
Please take a look at my code, maybe there is something wrong with it or its some setting with my Android emulator.
public static void store(Context c, String filename, int width, int height, int rooms, int expected_partiters, BSPNode root, Cells cells, int[][] dists, int startX, int startY)
{
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
// root elements
storeMaze(width, height, rooms, expected_partiters, root, cells, dists, startX, startY, doc);
String serializedDoc = MazeFileWriter.getStringFromDoc(doc);
try {
FileOutputStream fos = c.openFileOutput("data_test.xml", Context.MODE_PRIVATE);
fos.write(serializedDoc.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
}
static String getStringFromDoc(org.w3c.dom.Document doc) {
try
{
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
writer.flush();
return writer.toString();
}
catch(TransformerException ex)
{
ex.printStackTrace();
return null;
}
}
I am a beginner at Android, so any advice is appreciated!