保存/从文件中读取大型xml字符串
问题描述:
因此,我的应用程序从肥皂服务器接收大型xml。我希望将其保存在一个文件中,以备后用。我设法做到这一点,并阅读文件。但结果(阅读后)是一个乱码xml!来自xml后半部分的大部分文本(412个字符)被复制并粘贴到我的xml结尾,我无法弄清楚为什么会发生这种情况。 我已经尝试了2种方法来编写文件和2种方法来读取文件,没有骰子! (将在下面发布代码)注意:xml大5000-20000个字符,所以我使用方法来防止eclipse返回内存错误。保存/从文件中读取大型xml字符串
底线:
- 输入XML文件是正确的
- 输出XML文件不正确
-tried 2保存方法
-tried 2读取方法
- WTF?
保存代码1:
InputStream is = new ByteArrayInputStream(string.getBytes());
FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer))>0){
fos.write(buffer, 0, length);
}
fos.flush();
fos.close();
is.close();
保存代码2:
InputStream is = new ByteArrayInputStream(string.getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
Log.e("stuff is good", "line: "+line);
sb.append(line);
if (sb.toString().length() > 10000) {
fos.write(sb.toString().getBytes());
fos.flush();
sb = new StringBuilder();
}
}
fos.write(sb.toString().getBytes());
fos.flush();
is.close();
fos.close();
读取的代码1:
FileInputStream fis = openFileInput("caca");
int c;
StringBuilder fileContent = new StringBuilder();
while((c=fis.read())!=-1)
{
fileContent.append((char)c);
}
fis.close();
Log.e("TEST TEST", "XML length = "
+String.valueOf(fileContent.length()));
Log.e("TEST TEST", "XML = "
+fileContent);
读码2:
FileInputStream fis;
fis = openFileInput("caca");
StringBuffer fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
int i =1;
while (fis.read(buffer) != -1) {
fileContent.append(new String(buffer));
Log.v("TEST"+ String.valueOf(i), new String(buffer));
i++;
}
Log.e("TEST TEST", "XML length = "
+String.valueOf(fileContent.length()));
Log.e("TEST TEST", "XML = "
+fileContent);
保存到文件中的代码:
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(fileContent);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
对不起,长职位,但3天后,我在我束手无策。任何输入都会很好,谢谢!
答
ok ....我修好了,我不知道它为什么起作用。
保存代码:
public static void Save(String filename, String string,
Context ctx) {
Log.e("stuff is good", "xml length b4 save= "+String.valueOf(string.length()));
try {
FileOutputStream fOut = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(Login.messagesXmlDump);
myOutWriter.close();
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
读取的代码:
Save("LOL", messagesXmlDump, getApplicationContext());
try {
FileInputStream fis = openFileInput("LOL");
int c;
StringBuilder fileContent = new StringBuilder();
while((c=fis.read())!=-1)
{
fileContent.append((char)c);
}
fis.close();
托管到读/写一个70K字符长XML。也许这种挽救它的递归方法并不是最好的主意。认为我过分复杂了一件简单的事情。
对不起,浪费你的时间:(
答
我更喜欢使用Apache Commons IO此:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url + id);
try {
HttpResponse response = client.execute(httpGet);
InputStream content = response.getEntity().getContent();
StringWriter writer = new StringWriter();
IOUtils.copy(content, writer, "utf-8");
return writer.toString();
} catch (ClientProtocolException e) {
Log.e(tag, "client problem:" + e.getMessage());
throw new RuntimeException("client problem",e);
} catch (IOException e) {
Log.e(tag, "IO problem:" + e.getMessage());
throw new RuntimeException("IO problem",e);
}
然后就写出来的字符串如常。
对不起,323go,一个从我逃走了。我删除了其他6个或7不当Log.e的我之前发布的代码。 – Sebek 2013-03-28 09:31:13