Skip to Content

Java String to InputStream Example

  1. /*
  2.         Java String to InputStream Example.
  3.         This Java String to InputStream example shows how to convert Java String to InputStream.
  4.  */
  5.  
  6. import java.io.ByteArrayInputStream;
  7. import java.io.InputStream;
  8.  
  9. public class JavaStringToInputStreamExample {
  10.         public static void main(String args[]){
  11.                
  12.                 String str1 = "Java convert String to InputStream Example";
  13.                
  14.                 //convert string to bytes first using getBytes method of String class.
  15.                 byte[] bytes = str1.getBytes();
  16.                
  17.                 /*
  18.                  * To convert Java String to InputStream, use
  19.                  * ByteArrayInputStream class.
  20.                  */
  21.                
  22.                 InputStream inputStream = new ByteArrayInputStream(bytes);
  23.         }
  24. }