2013-01-23 45 views

回答

4
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(); 
    } 
} 

来源:How To Convert String To InputStream In Java

+0

+1阅读器的用法:) – MadProgrammer

+3

-1,用于在'str.getBytes()'中不指定'Charset'。 –

2

喜欢的东西...

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

应该工作...

0

可以使用ByteArrayInputStream。它使用InputStream方法读取byte[]中的元素。

0

对于InputStream MadProgrammer有答案。

如果Reader是确定的,那么你可以使用:

Reader r = StringReader(say); 
相关问题