我有一个名为bkup.xml的xml文件存储在sdcard“/sdcard/bkup.xml”中。从SD卡读取.xml文件
创建bkup.xml我已经使用了xmlSerialization。
我想从该bkup.xml文件检索数据。
我见过很多例子,但其中大部分例子都是使用资源文件并使用URL作为资源。但没有人给出sdcard文件路径的例子。
我不知道如何从该文件中获取数据并解析它。
在此先感谢。
任何建议将理解
我有一个名为bkup.xml的xml文件存储在sdcard“/sdcard/bkup.xml”中。从SD卡读取.xml文件
创建bkup.xml我已经使用了xmlSerialization。
我想从该bkup.xml文件检索数据。
我见过很多例子,但其中大部分例子都是使用资源文件并使用URL作为资源。但没有人给出sdcard文件路径的例子。
我不知道如何从该文件中获取数据并解析它。
在此先感谢。
任何建议将理解
使用的FileReader对象是这样的:
/**
* Fetch the entire contents of a text file, and return it in a String.
* This style of implementation does not throw Exceptions to the caller.
*
* @param aFile is a file which already exists and can be read.
* File file = new File(Environment.getExternalStorageDirectory() + "file path");
*/
static public String getContents(File aFile) {
//...checks on aFile are elided
StringBuilder contents = new StringBuilder();
try {
//use buffering, reading one line at a time
//FileReader always assumes default encoding is OK!
BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null; //not declared within while loop
/*
* readLine is a bit quirky :
* it returns the content of a line MINUS the newline.
* it returns null only for the END of the stream.
* it returns an empty String if two newlines appear in a row.
*/
while ((line = input.readLine()) != null){
contents.append(line);
contents.append(System.getProperty("line.separator"));
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
还需要添加权限访问设备上的SD卡。
发表你的代码你还有什么尝试过吗 – ingsaurabh 2012-02-13 12:25:43
你能告诉我任何接受文件位置解析路径的方法吗? – 2012-02-13 12:27:48