Java Bug?? String -> InputStream

本文介绍如何将XML格式的字符串转换为InputStream以生成Document对象。探讨了使用StringBufferInputStream、StringReader和ByteArrayInputStream等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

想把如XML格式的String转化成InputStream来生成一个Document

本想使用new StringBufferInputStream(string); 

Java API提示StringBufferInputStream已经deprecated,但可以使用StringReader class来生成stream,问题是我也看过StringReader的API,并没有提供和InputStream有关的方法。

难道这是一个BUG?

这里好像也有人提出过同样的问题。 http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4094886

 

最后只好用ByteArrayInputStream方法

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();

ByteArrayInputStream stream = new
ByteArrayInputStream(string.getBytes());
Document doc = docBuilder.parse(stream);
stream.close();
话说sun就不打算解决这种小的bug么?
当然,生成一个Document也可以使用InputSource类
Document document = docBuilder.parse(new InputSource(new StringReader(content)));
先生成一个StringReader,然后再构件一个InputSource,一样也可达成目的。 
收藏到: Del.icio.us
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值