2013-01-23 2 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); 
관련 문제