How to convert a String to an InputStream in Java?

Here ist a some example to explain how to convert a String to an InputStream in Java.

  • First example with using ByteArrayInputStream and BufferedReader
package com.nourit.javaIO;

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

public class StringToInputStream {

  public static void main(String[] args) {
    String value = "IT Training Center for Java and Web technology...";
    // convert String into InputStream
    InputStream inputStream = new ByteArrayInputStream(value.getBytes());
    // read it with BufferedReader
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

    String line;
    try {
      while ((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
      }
      bufferedReader.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
  • Second example with using ByteArrayInputStream
package com.nourit.javaIO;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

public class StringToInputStream2 {
  public static void main(String[] args) {
    String value = "IT Training Center for Java and Web technology...";
    InputStream inputStream;
    try {
      //convert it to an InputStream
      //For versions of Java less than 7, use "UTF-8"
      inputStream = new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8.name()));
      //convert it to a String back for read
      String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
      System.out.println(result);
      
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

 

  • Third example with using IOUtils.toInputStream
package com.nourit.javaIO;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

public class StringToInputStream1 {

  public static void main(String[] args) {
    String value = "IT Training Center for Java and Web technology...";
    InputStream inputStream;
    try {
      //convert it to an InputStream
      inputStream = IOUtils.toInputStream(value, "UTF-8");
      //convert it to a String back for read
      String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
      System.out.println(result);
      
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Output:

IT Training Center for Java and Web technology...

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*