我想检查android内部存储如果 new.xml
存在(将由我创建)然后它应该为我返回一个句柄,我可以很容易地向它添加新数据。如果它不存在,则创建一个新数据并向其添加数据。
我的xml文件结构就像这样简单。
<root>
<record>a</record>
<record>b</record>
<record>c</record>
</root>
如果文件在那里我只会向它添加一条新记录。但是如果不是我将创建一个新文件并将第一条记录添加到它。
以及我如何能够以一种方式读取这些数据 arraylist
。代码的一个例子非常感谢提前。
这很简单。这会对你有所帮助:
String filename = "file.txt";
FileOutputStream fos;
fos = openFileOutput(filename,Context.MODE_APPEND);
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, "UTF-8");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "root");
for(int j = 0 ; j < 3 ; j++)
{
serializer.startTag(null, "record");
serializer.text(data);
serializer.endTag(null, "record");
}
serializer.endDocument();
serializer.flush();
fos.close();
使用DOM解析器读回数据:
FileInputStream fis = null;
InputStreamReader isr = null;
fis = context.openFileInput(filename);
isr = new InputStreamReader(fis);
char[] inputBuffer = new char[fis.available()];
isr.read(inputBuffer);
data = new String(inputBuffer);
isr.close();
fis.close();
/*
* converting the String data to XML format
* so that the DOM parser understand it as an XML input.
*/
InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8"));
ArrayList<XmlData> xmlDataList = new ArrayList<XmlData>();
XmlData xmlDataObj;
DocumentBuilderFactory dbf;
DocumentBuilder db;
NodeList items = null;
Document dom;
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
dom = db.parse(is);
// normalize the document
dom.getDocumentElement().normalize();
items = dom.getElementsByTagName("record");
ArrayList<String> arr = new ArrayList<String>();
for (int i=0;i<items.getLength();i++){
Node item = items.item(i);
arr.add(item.getNodeValue());
}
这很简单。这会对你有所帮助:
String filename = "file.txt";
FileOutputStream fos;
fos = openFileOutput(filename,Context.MODE_APPEND);
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, "UTF-8");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "root");
for(int j = 0 ; j < 3 ; j++)
{
serializer.startTag(null, "record");
serializer.text(data);
serializer.endTag(null, "record");
}
serializer.endDocument();
serializer.flush();
fos.close();
使用DOM解析器读回数据:
FileInputStream fis = null;
InputStreamReader isr = null;
fis = context.openFileInput(filename);
isr = new InputStreamReader(fis);
char[] inputBuffer = new char[fis.available()];
isr.read(inputBuffer);
data = new String(inputBuffer);
isr.close();
fis.close();
/*
* converting the String data to XML format
* so that the DOM parser understand it as an XML input.
*/
InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8"));
ArrayList<XmlData> xmlDataList = new ArrayList<XmlData>();
XmlData xmlDataObj;
DocumentBuilderFactory dbf;
DocumentBuilder db;
NodeList items = null;
Document dom;
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
dom = db.parse(is);
// normalize the document
dom.getDocumentElement().normalize();
items = dom.getElementsByTagName("record");
ArrayList<String> arr = new ArrayList<String>();
for (int i=0;i<items.getLength();i++){
Node item = items.item(i);
arr.add(item.getNodeValue());
}
使用 getFilesDir() 获取可以创建文件的路径。通过创建文件对象来检查文件是否存在 File newXml = new File(getFilesDir+"/new.xml")
。然后检查它是否存在使用 if(newXml.exists())
。
要解析数据,请参阅 开发文档 。正如您在XmlPullParser示例中所看到的,它可以返回一个列表。
使用 openFileOutput 在内部存储和创建文件 getFileStreamPath 检查它们是否已存在并与它们一起工作
要将数据读入数组,只需打开文件流并使用任何适当的XML解析器