Introduction
In this post, we will learn how to convert String to InputStream in Java.
Example
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class StringToInputStream {
public static void main(String[] args) {
String text = "A quick fox jumps over a lazy dog";
try ( InputStream inputStream = convertToInputStream(text) ) {
//TODO you business logic goes here
} catch (Exception e) {
e.printStackTrace();
}
}
private static InputStream convertToInputStream(String text) throws Exception {
return new ByteArrayInputStream(text.getBytes());
}
}
Conclusion
This is the simplest and shortest way to convert a String to InputStream in Java.
Comments
Post a Comment