如何将String转成InputStream
下面的例子展示通过利用类ByteArrayInputStream将String转成InputStream。
1 package org.kodejava.example.io;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5
6 public class StringToStream {
7 public static void main(String[] args) {
8 String text = "Converting String to InputStream Example";
9
10 /*
11 * Convert String to InputString using ByteArrayInputStream class.
12 * This class constructor takes the string byte array which can be
13 * done by calling the getBytes() method.
14 */
15 try {
16 InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
17 } catch (UnsupportedEncodingException e) {
18 e.printStackTrace();
19 }
20 }
21 }
22
该例子来自:http://www.kodejava.org/examples/265.html
浙公网安备 33010602011771号