HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / I/O / Java String to InputStream

Java String to InputStream

Learn to convert Java String to InputStream using ByteArrayInputStream and IOUtils classes. Writing String to Steam is a frequent job in Java and having a couple of good shortcuts will prove useful.

Read More: Converting InputStream to String

1. ByteArrayInputStream

Using ByteArrayInputStream is simplest way to create InputStream from string. using this approach, we do not need any external dependency.

Example 1: Creating an InputStream from a String

The getBytes() method encodes the String into a sequence of bytes using the platform’s default charset. To use a different charset, use the method getBytes​(Charset charset).

The StandardCharsets class provides the constant definitions for the standard charsets. For example, StandardCharsets.UTF_8.

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

public class ConvertStringToInputStreamExample
{
   public static void main(String[] args)
   {
      String sampleString = "howtodoinjava.com";
	  
	  //Here converting string to inputstream
      InputStream stream = new ByteArrayInputStream(sampleString.getBytes());
   }
}

2. IOUtils – Apache Commons IO

IOUtils is very useful class for IO operations in Java. This solution is also pretty good because apache commons is very much a mostly included jar in most applications.

IOUtils.toInputStream() makes code very much readable. It is an overloaded method so use the desired encoding while invoking this method.

static InputStream toInputStream(String input)    //Deprecated
static InputStream toInputStream(String input, Charset encoding)
static InputStream toInputStream(String input, String encoding)

Example 2: How to create InputStream from a String in Java

import java.io.InputStream;
import org.apache.commons.io.IOUtils;

public class ConvertStringToInputStreamExample
{
   public static void main(String[] args)
   {
      String sampleString = "howtodoinjava.com";
	  
      //Here converting the string to the inputstream
      InputStream stream = IOUtils.toInputStream(sampleString, StandardCharsets.UTF_8);
   }
}

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. WolframG

    July 17, 2015

    I hoped that solution #2 would be a bit more memory efficient than to duplicate the entire string as byte array. But when looking at the sources it turns out that #1 is exactly what is used inside #2.

    • Lokesh Gupta

      July 17, 2015

      🙂 So it turned out to be only little more readable code.

Comments are closed on this article!

Search Tutorials

Java IO

  • Java IO Introduction
  • Java How IO works?
  • Java IO vs NIO
  • Java Create File
  • Java Write to File
  • Java Append to File
  • Java Read File
  • Java Read File to String
  • Java Read File to Byte[]
  • Java Make File Read Only
  • Java Copy File
  • Java Copy Directory
  • Java Delete Directory
  • Java Current Working Directory
  • Java Read/Write Properties File
  • Java Read File from Resources
  • Java Read File from Classpath
  • Java Read/Write UTF-8 Data
  • Java Check if File Exist
  • Java Create Temporary File
  • Java Write to Temporary File
  • Java Delete Temporary File
  • Java Read from Console
  • Java Typesafe input using Scanner
  • Java Password Protected Zip
  • Java Unzip with Subdirectories
  • Java Generate SHA/MD5
  • Java Read CSV File
  • Java InputStream to String
  • Java String to InputStream
  • Java OutputStream to InputStream
  • Java InputStreamReader
  • Java BufferedReader
  • Java FileReader
  • Java LineNumberReader
  • Java StringReader
  • Java FileWriter
  • Java BufferedWriter
  • Java FilenameFilter
  • Java FileFilter

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces