How to Convert InputStream Java in the string? [repeat]

This translation from: How do the I Convert For A AN InputStream to String in the Java [Duplicate]?

This question already has an answer here: This question has been answered here:

Given a string: Given a string:

String exampleString = "example";

Convert For IT to do the I How AN InputStream? How to convert it to InputStream?


#1st Floor

Reference: https://stackoom.com/question/3HTm/ how InputStream- repeat string will convert Java


#2nd Floor

Like this: Like this:

InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));

Assumes that the this that you note want IS AN that the InputStream Stream A Original of bytes that Represent your AS String encoded UTF-8 . Note that this assumes that you need a the InputStream, which is a byte stream that represents the encoded as UTF-8 the original string.

Within last less Java versions of the For 7, the replace StandardCharsets.UTF_8with "UTF-8". For Java version is less than version 7, it will be StandardCharsets.UTF_8replaced "UTF-8".


#3rd floor

You can use StringReader, and use this other stackoverflow post solutions to convert the reader to the input stream.


#4th floor

A using the Find that the I Apache Commons IO Makes My Life Easier much. I found that using Apache Commons IO to make my life easier.

String source = "This is the source of my input stream";
InputStream in = org.apache.commons.io.IOUtils.toInputStream(source, "UTF-8");

You may find that the library also offer many other shortcuts to commonly done tasks that you may be able to use in your project. You may find that the library also offers a number of other shortcuts you can use these common tasks in the project.

Original articles published 0 · won praise 73 · views 560 000 +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105385187