String转换为InputStream

Jobernowl 2012-07-02 12:10:45
各位我想把String转换为InputStream
我在网上搜了很久 发现人家都是这么写

String content="esff绯闻绯闻";
ByteArrayInputStream in=new ByteArrayInputStream(content.getBytes());


但是我发现这么写是有问题的
因为这个String其实是从外部来的 我根本不知道它的编码方式(经过观察,有多种编码方式),所以以上那个方法是不行的

然后我又发现有一个类

String content="esff绯闻绯闻";
StringBufferInputStream in=new StringBufferInputStream(content);


但是这个类已经过时了 并且好像不支持中文

由于需求实现方面的问题 必须要得到inputStream 所以不知道怎么把String转换为InputStream 求解答
...全文
340 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jobernowl 2012-07-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

ant.jar

StringInputStream
[/Quote]


感谢提点 我稍后看看
coooliang 2012-07-03
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
不知道编码方式?已经变成 String 对象的话,就使用 Java 所有的 Unicode 了。
周靖峰 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:

ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));


你这个ISO-8859-1是从何得来 为什么不是utf-8、gbk、unicode
[/Quote]

写成ISO-8859-1的话,就是用ISO-8859-1编码解析字符串获得字节流

如果你什么都不写,得到的字节流的编码格式就是系统默认的编码格式,windows下是gbk
Jobernowl 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
[/Quote]

你这个ISO-8859-1是从何得来 为什么不是utf-8、gbk、unicode
a395885670 2012-07-02
  • 打赏
  • 举报
回复
ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
brightyq 2012-07-02
  • 打赏
  • 举报
回复
对,是这样转。
Jobernowl 2012-07-02
  • 打赏
  • 举报
回复
其实我的流程是这样子的

原始文件(假设是big5编码)first -->以utf-8方式读取出来(变为乱码)content 现在我要得到原来的原始数据

请问该怎么做? new String(content.getBytes("utf-8"),"big5")?

求大神指教
Jobernowl 2012-07-02
  • 打赏
  • 举报
回复
看来确实如此 一旦有了String这个内容确实不能还原回去了 其实我就是想还原回去 看来是不可能了
brightyq 2012-07-02
  • 打赏
  • 举报
回复
String content="esff绯闻绯闻";
ByteArrayInputStream in=new ByteArrayInputStream(content.getBytes());


楼主这种写法没有问题,content.getBytes(),可以使用字符集编码,比如content.getBytes("UTF-8"),然后从流再转回String时,再使用UTF-8解码就可以了,还是当初你的content的内容,不会出现乱码。

也就是说转成流,用哪个编码都可以,重点是采用相同的字符集解码,得到当初的String,毕竟我们可以看的到的是String,不是流。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧