如何在Java中将字符串读入inputStream?
可能重复:
如何在Java中将字符串转换为InputStream?
我如何在Java中将一个字符串读入InputStream?
我希望能够将String say = "say"
转换为InputStream / InputSource。 我怎么做?
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
就像是...
InputStream is = new ByteArrayInputStream(sValue.getBytes());
应该管用...
您可以使用ByteArrayInputStream
。 它使用InputStream
方法从byte[]
读取元素。
上一篇: How can I read a String into an inputStream in Java?
下一篇: Easy way to write contents of a Java InputStream to an OutputStream