【问题标题】:How to exchange FileOutputStream for BufferedOutputStream如何将 FileOutputStream 交换为 BufferedOutputStream
【发布时间】:2011-06-04 10:57:58
【问题描述】:

我用这种方法将一个xml文件序列化到SD卡

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

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

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

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

也许如果我将此方法与 BufferedOutputStream 而不是 FileOutputStream 一起使用... 但我不知道如何调整代码以使用缓冲区

我的问题是如何向这段代码添加缓冲区。

这是我得到的输出日志:

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)

【问题讨论】:

  • 这显然是 XML 编码的问题,而不是 I/O 的问题。你的问题是错误的。
  • 好吧,我问了一个明确的问题,如何更改 I/O 模式,我的问题是否不同都没关系。如果有人来到这个站点并搜索如何从 FileoutputStream 更改为 BufferedOutputStream 它会找到答案。一件事是我的问题,另一件事是如果我提出了明确的问题并找到了正确的答案。
  • 您发布的所有信息都与 XML 问题有关,并且无法通过更改 I/O 模式来解决,这本身就是微不足道的。你的最后一句话毫无意义。如果您的问题与您的问题无关,那么您应该改写它。

标签: java android xmlserializer fileoutputstream


【解决方案1】:

根据xmlpull docssetOutput function 将一个 Writer 类的实例和一个用于编码的字符串作为参数。因此,您也可以使用 BufferedWriter,而不是使用 BufferedOutputStream。尝试将第 47 行和第 49 行替换为以下内容:

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

【讨论】:

  • 你有什么输出吗?异常/警告/错误?
  • 是第 88 行中的那个,但起初你在第 57 行的代码给了我一个错误,因为 fileos 没有初始化所以我更改了 BufferedWriter fileos;对于 BufferedWritter fileos = null;然后......什么都没有
  • 试试PrintWriter fileos = null;fileos = new PrintWriter(new BufferedWriter(new FileWriter(newxmlfile)));
  • 我已经尝试了你所说的但没有运气,第二行给了我一个错误构造函数 PrintWriter(BufferedWriter) 未定义
  • BufferedWriter doc 使用 BufferedWriter 的实例初始化 PrintWriter,但 PrintWriter doc 仅提及 writer 类。你可以试试fileos = new PrintWriter(new FileWriter(newxmlfile));,但我认为你不会有运气。对于日志:我无法告诉您问题出在哪里,但是在 serializartodo 中,您可能使用 getAttributeValue 超出了一些数组
【解决方案2】:

只需替换行

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

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

【讨论】:

  • 您要保存的文件有多大,“停止工作”是什么意思?您是否在文本视图中看到“文件已在 SD 卡上创建”消息?
  • 不,我有异常(第 88 行)XML 文件不是那么大,但我认为问题与大小有关,因为我尝试过使用较小的 XML 文件和一切顺利。即,如果我删除一行序列化会更进一步。
  • 您可能只是对序列化程序做错了。如果您将代码中的日志消息更改为 Log.e("Exception","error occurred while creating xml file", e);,您将获得完整的堆栈跟踪,并可以在此处发布。
  • 检查您的 xml 格式是否正确。您将在 xml2html.java 第 273 行附近获得 IndexOutOfBounds。发布您的真实来源可能会有所帮助。
  • xml 格式正确,这一点毫无疑问。我知道,因为如果我把它切成小块,每一块都可以工作。所以这与 xml 表单无关。它一定是别的东西。
猜你喜欢
  • 2020-07-15
  • 2017-09-17
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 2012-02-01
  • 2019-09-06
相关资源
最近更新 更多