How to convert String to InputStream in Java

1 Answer

0 votes
package javaapplication1;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class JavaApplication1 {
    
    public static void main(String[] args) throws IOException {
  
        String s = "java php c++ PHP";
        int i;
        char ch;
         
        byte[] bytes = s.getBytes();

        InputStream inputStream = new ByteArrayInputStream(bytes);
       
        while ( (i=inputStream.read()) != -1)
        {
            ch = (char)i;
            System.out.print(ch);
        }
    }    
}
   
/*
   
run:
   
java php c++ PHP
   
*/

 



answered Oct 27, 2016 by avibootz

Related questions

2 answers 240 views
3 answers 363 views
3 answers 347 views
1 answer 190 views
1 answer 268 views
...