How do I convert a String to an InputStream in Java?
Like this:
InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8.
For versions of Java less than 7, replace StandardCharsets.UTF_8
with "UTF-8"
.
How do I read / convert an InputStream into a String in Java?
A nice way to do this is using Apache commons IOUtils
to copy the InputStream
into a StringWriter
... something like
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();
or even
// NB: does not close inputStream, you'll have to use try-with-resources for that
String theString = IOUtils.toString(inputStream, encoding);
Alternatively, you could use ByteArrayOutputStream
if you don't want to mix your Streams and Writers
How to convert a String[] array to InputStream in Java
You can construct a merged String
with some separator and then to byte[]
and then to ByteArrayInputStream
.
Here's a way to convert a String to InputStream in Java.
How to convert a String to an InputStream in Kotlin?
Kotlin has an extension for String
to convert directly.
val inputStream: InputStream = myString.byteInputStream()
The argument on byteInputStream
is defaulted to charset: Charset = Charsets.UTF_8
.
You can look at the extension by writing it and then cmd+click on it or in the package kotlin.io
file IOStream.kt
Relying on the Java version is not wrong, but rather using a more kotlin idiomatic way when possible
How do I turn a String into a InputStreamReader in java?
ByteArrayInputStream also does the trick:
InputStream is = new ByteArrayInputStream( myString.getBytes( charset ) );
Then convert to reader:
InputStreamReader reader = new InputStreamReader(is);
How does one create an InputStream from a String?
Here you go:
InputStream is = new ByteArrayInputStream( myString.getBytes() );
Update For multi-byte support use (thanks to Aaron Waibel's comment):
InputStream is = new ByteArrayInputStream(Charset.forName("UTF-16").encode(myString).array());
Please see ByteArrayInputStream manual.
It is safe to use a charset argument in String#getBytes(charset) method above.
After JDK 7+ you can use
java.nio.charset.StandardCharsets.UTF_16
instead of hardcoded encoding string:
InputStream is = new ByteArrayInputStream(StandardCharsets.UTF_16.encode(myString).array());
How can I convert a String into a DataInputStream type in Java?
This might help
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
public class MyApp {
public static void main(String[] args) throws Exception {
InputStream is = new ByteArrayInputStream("hi I am test".getBytes(Charset.forName("UTF-8")));
DataInputStream dataIn = new DataInputStream(is);
while (dataIn.available() > 0) {
String k = dataIn.readLine();
System.out.print(k + " ");
}
}
}
Java: accessing a List of Strings as an InputStream
You can concatenate all the lines together to create a String then convert it to a byte array using String#getBytes
and pass it into ByteArrayInputStream. However this is not the most efficient way of doing it.
Related Topics
Differences Between System.Out.Println() and Return in Java
Java.Lang.Classnotfoundexception in Spite of Using Classpath Environment Variable
What Is the Purpose of Java's Unary Plus Operator
Getting Fonts, Sizes, Bold,...Etc
How to Terminate Scanner When Input Is Complete
Comparing Two Integer Arrays in Java
How to Store More Than One String in a Map
How to Create a Directory in Java
Java 8: Performance of Streams VS Collections
Differencebetween Swing and Awt
Sockets: Discover Port Availability Using Java
How to Delete Stuff Printed to Console by System.Out.Println()
Interfaces with Static Fields in Java for Sharing 'Constants'
Why Does Hibernate Require No Argument Constructor