簡體   English   中英

如何將 FileOutputStream 交換為 BufferedOutputStream

[英]How to exchange FileOutputStream for BufferedOutputStream

我已經使用這種方法將 xml 文件序列化到 SD 卡

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

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

問題與大小有關,我的意思是 XML 文件大小

因為如果我使用較小的文件,它可以正常工作

也許如果我將此方法與 BufferedOutputStream 而不是 FileOutputStream 一起使用會起作用...但我不知道如何調整代碼以使用緩沖區

Ant 這是我的問題,如何在此代碼中添加緩沖區。

這是我得到的 output 日志:

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)

根據xmlpull 文檔setOutput function將 Writer 類的實例和用於編碼的字符串作為參數。 因此,您也可以使用 BufferedWriter,而不是使用 BufferedOutputStream。 嘗試將第 47 行和第 49 行替換為以下內容:

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

只需替換行

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM