Java StringBuffer to InputStream Example /*         Java StringBuffer to InputStream Example         This example shows ...

Java StringBuffer to InputStream Example

02:42 PASSOVE INCOME 0 Comments

Java StringBuffer to InputStream Example

  1. /*
  2.         Java StringBuffer to InputStream Example
  3.         This example shows how to convert StringBuffer to InputStream in Java using
  4.         ByteInputStream class.
  5. */
  6. import java.io.ByteArrayInputStream;
  7. import java.io.InputStream;
  8. public class StringBufferToInputStreamExample {
  9.        
  10.         public static void main(String args[]){
  11.                
  12.                 //create StringBuffer object
  13.                 StringBuffer sbf = new StringBuffer("StringBuffer to InputStream Example");
  14.                
  15.                 /*
  16.                  * To convert StringBuffer to InputStream in Java, first get bytes
  17.                  * from StringBuffer after converting it into String object.
  18.                  */
  19.                
  20.                 byte[] bytes = sbf.toString().getBytes();
  21.                
  22.                 /*
  23.                  * Get ByteArrayInputStream from byte array.
  24.                  */
  25.                
  26.                 InputStream inputStream = new ByteArrayInputStream(bytes);
  27.                
  28.                 System.out.println("StringBuffer converted to InputStream");
  29.         }
  30. }

0 comments: