好的,我认为这会更简单,但我可能错了..我的解析器工作正常(直接与连接工作)所以我在 Android(又名 Java)上或使用输入/输出上写入和读取文件时遇到了一些基本问题流..问题是我不知道什么不起作用..我错过了任何许可?我在写入或读取文件时遗漏了什么?
这是代码:
URL url = new URL(urlXml);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader reader = sp.getXMLReader();
CustomXmlHandler handler = new CustomXmlHandler();
reader.setContentHandler((ContentHandler) handler);
String fileName = getFileName();
File f = getFileStreamPath(fileName);
if(!f.exists()) {
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
URLConnection conn = url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(10000);
InputStream in = conn.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
in.close();
}
FileInputStream fis = openFileInput(fileName);
reader.parse(new InputSource(new InputStreamReader(fis)));
fis.close();
myObject = handler.getMyObject();
提前谢谢..我快疯了..