如何为BufferedOutputStream交换FileOutputStream

时间:2022-10-30 20:27:34

I've used this method to serialize an xml file to a SD card

我已经使用这种方法将xml文件序列化为SD卡

http://www.anddev.org/write_a_simple_xml_file_in_the_sd_card_using_xmlserializer-t8350.html

it works fine but at some point it stops working

它工作正常,但在某些时候它停止工作

The issue is size related, the XML file size I mean

问题与大小有关,我的意思是XML文件大小

because if I use smaller files it works fine

因为如果我使用较小的文件,它工作正常

maybe if i use this method with a BufferedOutputStream instead of FileOutputStream will work... but I don't know how to adapt the code for working with buffer

也许如果我使用这个方法与BufferedOutputStream而不是FileOutputStream将工作...但我不知道如何调整代码使用缓冲区

Ant that's my question how to add a buffer to this code.

Ant这是我的问题如何为此代码添加缓冲区。

This is the output log I've got:

这是我得到的输出日志:

06-04 14:06:09.754: ERROR/Exception(2279): error occurred while creating xml file
06-04 14:06:09.754: ERROR/Exception(2279): java.lang.IndexOutOfBoundsException
06-04 14:06:09.754: ERROR/Exception(2279):     at org.kxml2.io.KXmlParser.getAttributeValue(KXmlParser.java:1303)
06-04 14:06:09.754: ERROR/Exception(2279):     at com.digitalnatura.htmlgenereitor.xml2html.serializartodo(xml2html.java:273)
06-04 14:06:09.754: ERROR/Exception(2279):     at com.digitalnatura.htmlgenereitor.xml2html.onCreate(xml2html.java:44)
06-04 14:06:09.754: ERROR/Exception(2279):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-04 14:06:09.754: ERROR/Exception(2279):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-04 14:06:09.754: ERROR/Exception(2279):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-04 14:06:09.754: ERROR/Exception(2279):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-04 14:06:09.754: ERROR/Exception(2279):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-04 14:06:09.754: ERROR/Exception(2279):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 14:06:09.754: ERROR/Exception(2279):     at android.os.Looper.loop(Looper.java:123)
06-04 14:06:09.754: ERROR/Exception(2279):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-04 14:06:09.754: ERROR/Exception(2279):     at java.lang.reflect.Method.invokeNative(Native Method)
06-04 14:06:09.754: ERROR/Exception(2279):     at java.lang.reflect.Method.invoke(Method.java:521)
06-04 14:06:09.754: ERROR/Exception(2279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-04 14:06:09.754: ERROR/Exception(2279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-04 14:06:09.754: ERROR/Exception(2279):     at dalvik.system.NativeStart.main(Native Method)

2 个解决方案

#1


1  

Just replace the lines

只需更换线条

FileOutputStream fileos = null;
// ...
fileos = new FileOutputStream(newxmlfile);

with

OutputStream fileos = null;
// ...
fileos = new BufferedOutputStream(new FileOutputStream(newxmlfile));

#2


2  

According to the xmlpull docs, the setOutput function takes an instance of a Writer-class and a String for encoding as parameters. So instead of using the BufferedOutputStream, you could also use the BufferedWriter. Try to replace the lines 47 and 49 with the following:

根据xmlpull文档,setOutput函数接受Writer类的实例和String作为参数进行编码。因此,您也可以使用BufferedWriter,而不是使用BufferedOutputStream。尝试用以下内容替换第47和49行:

47  BufferedWriter fileos = null;
49  fileos = new BufferedWriter(new FileWriter(newxmlfile));

#1


1  

Just replace the lines

只需更换线条

FileOutputStream fileos = null;
// ...
fileos = new FileOutputStream(newxmlfile);

with

OutputStream fileos = null;
// ...
fileos = new BufferedOutputStream(new FileOutputStream(newxmlfile));

#2


2  

According to the xmlpull docs, the setOutput function takes an instance of a Writer-class and a String for encoding as parameters. So instead of using the BufferedOutputStream, you could also use the BufferedWriter. Try to replace the lines 47 and 49 with the following:

根据xmlpull文档,setOutput函数接受Writer类的实例和String作为参数进行编码。因此,您也可以使用BufferedWriter,而不是使用BufferedOutputStream。尝试用以下内容替换第47和49行:

47  BufferedWriter fileos = null;
49  fileos = new BufferedWriter(new FileWriter(newxmlfile));