How can I read a String into an inputStream in Java?

Possible Duplicate:
How do I convert a String to an InputStream in Java?

How can I read a String into an InputStream in Java ?

I want to be able to convert String say = "say" into an InputStream/InputSource. How do I do that?


public class StringToInputStreamExample {
    public static void main(String[] args) throws IOException {
    String str = "This is a String ~ GoGoGo";

    // convert String into InputStream
    InputStream is = new ByteArrayInputStream(str.getBytes());

    // read it with BufferedReader
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }

    br.close();
   }
}

来源:如何将字符串转换为Java中的InputStream


Something like...

InputStream is = new ByteArrayInputStream(sValue.getBytes());

Should work...


You can use the ByteArrayInputStream . It reads elements from a byte[] with the InputStream methods.

链接地址: http://www.djcxy.com/p/78462.html

上一篇: 如何在Java中将String []数组转换为InputStream

下一篇: 如何在Java中将字符串读入inputStream?