How can I get a java.io.InputStream from a java.lang.String?

前端 未结 8 814
悲&欢浪女
悲&欢浪女 2020-12-23 20:23

I have a String that I want to use as an InputStream. In Java 1.0, you could use java.io.StringBufferInputStream, but that has been @Deprecra

8条回答
  •  隐瞒了意图╮
    2020-12-23 20:36

    Well, one possible way is to:

    • Create a PipedOutputStream
    • Pipe it to a PipedInputStream
    • Wrap an OutputStreamWriter around the PipedOutputStream (you can specify the encoding in the constructor)
    • Et voilá, anything you write to the OutputStreamWriter can be read from the PipedInputStream!

    Of course, this seems like a rather hackish way to do it, but at least it is a way.

提交回复
热议问题